I have been trying to create an array of a class containing two values, but when I try to apply a value to the array I get a NullPointerException.
public class ResultLi开发者_如何学Pythonst {
public String name;
public Object value;
}
public class Test {
public static void main(String[] args){
ResultList[] boll = new ResultList[5];
boll[0].name = "iiii";
}
}
Why am I getting this exception and how can I fix it?
You created the array but didn't put anything in it, so you have an array that contains 5 elements, all of which are null. You could add
boll[0] = new ResultList();
before the line where you set boll[0].name.
ResultList[] boll = new ResultList[5];
creates an array of size=5, but does not create the array elements.
You have to instantiate each element.
for(int i=0; i< boll.length;i++)
boll[i] = new ResultList();
As many have said in the previous answers, ResultList[] boll = new ResultList[5];
simply creates an array of ResultList having size 5 where all elements are null. When you are using boll[0].name
, you are trying to do something like null.name
and that is the cause of the NullPointerException. Use the following code:
public class Test {
public static void main(String[] args){
ResultList[] boll = new ResultList[5];
for (int i = 0; i < boll.length; i++) {
boll[i] = new ResultList();
}
boll[0].name = "iiii";
}
}
Here the for loop basically initializes every element in the array with a ResultList
object, and once the for loop is complete, you can use
boll[0].name = "iiii";
I think by calling
ResultList[] boll = new ResultList[5];
you created an array which can hold 5 ResultList, but you have to initialize boll[0]
before you can set a value.
boll[0] = new ResultList();
ResultList p[] = new ResultList[2];
By writing this you just allocate space for a array of 2 elements. You should initialize the reference variable by doing this:
for(int i = 0; i < 2; i++){
p[i] = new ResultList();
}
Furthermore, you can prove this to yourself by adding a debug line to your class, such as:
public class ResultList {
public String name;
public Object value;
public ResultList() {
System.out.println("Creating Class ResultList");
}
}
Whenever an object is created, one of its constructors must be called (if there is no constructor, a default one is created automatically, similar to the one you already have in your class). If you only have one constructor, then the only way to create an object is to call that constructor. If the line
ResultList[] boll = new ResultList[5];
really created 5 new objects, you would see your debug line appear on the console 5 times. If it does not, you know the constructor is not being called. Notice also that the above line does not have a parameter list with open and close parentheses "()" so it is not a function call - or constructor call. Instead, we are only referring to the type. We are saying: "I need space for an array of ResultList objects, up to 5 total." After this line, all you have is empty space, not objects.
As you try out various fixes, the debug line will help confirm you are getting what you want.
class ResultList {
public String name;
public Object value;
public ResultList() {}
}
public class Test {
public static void main(String[] args){
ResultList[] boll = new ResultList[5];
boll[0] = new ResultList(); //assign the ResultList objet to that index
boll[0].name = "iiii";
System.out.println(boll[0].name);
}
}
Till you have created the ResultSet Object but every index is empty that is pointing to null reference that is the reason you are getting null. So just assign the Object on that index and then set the value.
first of all you have created 5 element of ResultList type, but when inserting value you are inserting name and value wrong. you could use constructor to create and insert values to the array elements.
class ResultList {
public String name;
public Object value;
public ResultList(String name,Object value){
this.name = name;
this.value = value;
System.out.println(name+" --- "+value);
}
}
public static void main(String[] args) {
ResultList[] boll = new ResultList[5];
boll[0] = new ResultList("myName","myValue");
}
Either you can try this scenario or you can the make the variable "name" static in ResultList Class. So when the ResultList[] boll = new ResultList[5]; gets executed at that time all the variable from that class will gets assign
public static void main(String[] args){
ResultList[] boll = new ResultList[5];
boll[0] = new ResultList();
boll[0].name = "iiii";
System.out.println(boll[0].name);
}
public class ResultList {
public static String name;
public Object value;
public ResultList() {}
}
精彩评论