当我运行这段代码,返回数组中的Java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
class Posting
{
String title;
}
public class Test
{
Posting[] dew()
{
Posting[] p = new Posting[100];
for(int i = 0; i <p.length; i++)
{
p[i].title = "this is " + i;
}
return p;
}
public static void main(String args[])
{
Test t = new Test();
Posting[] out = t.dew();
for(int i = 0; i < out.length; i ++)
{
System.out.println(out[i].title);
}
}
}
我得到这个错误, 运行:
Exception in thread "main" java.lang.NullPointerException
at mistAcademic.javaProject.newsBot.core.Test.dew(Test.java:20)
at mistAcademic.javaProject.newsBot.core.Test.main(Test.java:29)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)
莫非你有什么想法?
数组是空的。未创建发布对象如何访问其标题字段 – KDjava