此函数中的Nullpointerexception [英] Nullpointerexception in this function
问题描述
Hello CodeProject社区,
我正在尝试实例化一个元素矩阵。但是当我调用矩阵中的某个元素时,它会返回Null(或者在调用方法时返回NullPointerException)。这是我的代码的简化版本,仍然返回Null。我想知道导致这个问题的原因。谢谢!
祝你好运
我的尝试: < br $> b $ b
public class MyProgram extends ConsoleProgram
{
public void run()
{
someClass [] [] sc = new someClass [< span class =code-digit> 1 ] [ 2 ];
System.out.println(sc [ 0 ] [ 0 ]。getX()) ;
}
}
public class someClass
{
private int x = 0 跨度>;
public someClass()
{
}
public int getX()
{
return x;
}
}
你的问题在于功能运行
这就是你所做的事情
public void run()
{
someClass [] [] sc = new someClass [ 1 ] [ 2 ]; // 您已为数组本身分配了一些空间;
System.out.println( sc [ 0 ] [ 0 ]。getX()); // 访问此处是违规行为
/ / 因为你没有为每个对象初始化空间;因此,在你开始访问之前,你需要初始化。
}
换句话说
< pre lang =java> sc [ 0 ] [ 0 ] = new someClass();
当声明一个原始变量数组时,java会为该变量设置默认值。对于所有类和String,默认为null。按照示例更好地理解
public class Klass {
public int x = 10;
public static void main ( String [] args){
int [] i = new int [ 2 ];
char [] c = new char [ 2 跨度>];
Klass [] k = new Klass [ 2 ];
字符串 s [] = new String [ 2 ];
System.out.println(i [ 0 ] + + i [ 1 ]);
System.out.println(c [ 0 ] + + c [ 1 ]); // 在这种情况下,c设置为零
System.out.println(s [ 0 ] + + s [ 1 ]);
System.out.println(k [ 0 ] + + k [ 1 ]);
尝试 {
System.out.println(k [ 0 ]。x ); // 因为k为null,它将引发异常
} catch (NullPointerException e){
e.printStackTrace();
}
k [ 0 ] = new Klass();
System.out.println(k [ 0 ]。x); // 您会看到一些结果
}
}
Hello CodeProject community,
I'm trying to instantiate a matrix of an element. But when I call a certain element in the matrix, it would return a Null (or a NullPointerException when calling a method). Here's a simplified version of my code which still returns Null. I wonder what causes this problem. Thank you!
Best regards
What I have tried:
public class MyProgram extends ConsoleProgram
{
public void run()
{
someClass[][] sc = new someClass[1][2];
System.out.println(sc[0][0].getX());
}
}
public class someClass
{
private int x = 0;
public someClass()
{
}
public int getX()
{
return x;
}
}
your problem is with function run
This is what you have done
public void run() { someClass[][] sc = new someClass[1][2]; // you have allocated some space for the array itself; System.out.println(sc[0][0].getX()); // accessing here is a violation // since you have not initialize space for each object; hence before you start accessing you need to initialize. }
in other word
sc[0][0] = new someClass();
When you declare an array of primitive variables, java set default value for that variable. And for all classes and String default is null. Follow the example to understand better
public class Klass { public int x=10; public static void main(String []args) { int []i = new int[2]; char []c=new char[2]; Klass []k=new Klass[2]; String s[] = new String[2]; System.out.println(i[0] + " " + i[1]); System.out.println(c[0] + " " + c[1]); // in this case c is set to zero System.out.println(s[0] + " " + s[1]); System.out.println(k[0] + " " + k[1]); try { System.out.println(k[0].x); // since k is null it will thow exception } catch(NullPointerException e) { e.printStackTrace(); } k[0] = new Klass(); System.out.println(k[0].x); // You will see some result } }
这篇关于此函数中的Nullpointerexception的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!