NullPointerException when Creating an Array of objects [duplicate]












30
















This question already has an answer here:




  • What is a NullPointerException, and how do I fix it?

    12 answers




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 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].name = "iiii";
}
}


Why am I getting this error and how can I fix it?










share|improve this question















marked as duplicate by Makoto java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Mar 7 '18 at 3:06


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • It might be a little clearer to rename ResultList to something like NameValuePair.

    – Nathan Hughes
    Dec 17 '09 at 15:59











  • You know that a class with bind key/value already exists? implementations of Map<String, Object> for instance.

    – enguerran
    Dec 17 '09 at 16:18






  • 4





    A common gotcha coming from C++, the new array is an array of references, but the actual objects are not created. You have to create them as a separate step. Java does NOT allow an array of Objects like C++ does.

    – Peter Lawrey
    Dec 17 '09 at 21:27
















30
















This question already has an answer here:




  • What is a NullPointerException, and how do I fix it?

    12 answers




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 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].name = "iiii";
}
}


Why am I getting this error and how can I fix it?










share|improve this question















marked as duplicate by Makoto java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Mar 7 '18 at 3:06


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • It might be a little clearer to rename ResultList to something like NameValuePair.

    – Nathan Hughes
    Dec 17 '09 at 15:59











  • You know that a class with bind key/value already exists? implementations of Map<String, Object> for instance.

    – enguerran
    Dec 17 '09 at 16:18






  • 4





    A common gotcha coming from C++, the new array is an array of references, but the actual objects are not created. You have to create them as a separate step. Java does NOT allow an array of Objects like C++ does.

    – Peter Lawrey
    Dec 17 '09 at 21:27














30












30








30


4







This question already has an answer here:




  • What is a NullPointerException, and how do I fix it?

    12 answers




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 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].name = "iiii";
}
}


Why am I getting this error and how can I fix it?










share|improve this question

















This question already has an answer here:




  • What is a NullPointerException, and how do I fix it?

    12 answers




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 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].name = "iiii";
}
}


Why am I getting this error and how can I fix it?





This question already has an answer here:




  • What is a NullPointerException, and how do I fix it?

    12 answers








java arrays nullpointerexception






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 28 '12 at 9:23







user166390

















asked Dec 17 '09 at 15:49









marjasinmarjasin

153124




153124




marked as duplicate by Makoto java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Mar 7 '18 at 3:06


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Makoto java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Mar 7 '18 at 3:06


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • It might be a little clearer to rename ResultList to something like NameValuePair.

    – Nathan Hughes
    Dec 17 '09 at 15:59











  • You know that a class with bind key/value already exists? implementations of Map<String, Object> for instance.

    – enguerran
    Dec 17 '09 at 16:18






  • 4





    A common gotcha coming from C++, the new array is an array of references, but the actual objects are not created. You have to create them as a separate step. Java does NOT allow an array of Objects like C++ does.

    – Peter Lawrey
    Dec 17 '09 at 21:27



















  • It might be a little clearer to rename ResultList to something like NameValuePair.

    – Nathan Hughes
    Dec 17 '09 at 15:59











  • You know that a class with bind key/value already exists? implementations of Map<String, Object> for instance.

    – enguerran
    Dec 17 '09 at 16:18






  • 4





    A common gotcha coming from C++, the new array is an array of references, but the actual objects are not created. You have to create them as a separate step. Java does NOT allow an array of Objects like C++ does.

    – Peter Lawrey
    Dec 17 '09 at 21:27

















It might be a little clearer to rename ResultList to something like NameValuePair.

– Nathan Hughes
Dec 17 '09 at 15:59





It might be a little clearer to rename ResultList to something like NameValuePair.

– Nathan Hughes
Dec 17 '09 at 15:59













You know that a class with bind key/value already exists? implementations of Map<String, Object> for instance.

– enguerran
Dec 17 '09 at 16:18





You know that a class with bind key/value already exists? implementations of Map<String, Object> for instance.

– enguerran
Dec 17 '09 at 16:18




4




4





A common gotcha coming from C++, the new array is an array of references, but the actual objects are not created. You have to create them as a separate step. Java does NOT allow an array of Objects like C++ does.

– Peter Lawrey
Dec 17 '09 at 21:27





A common gotcha coming from C++, the new array is an array of references, but the actual objects are not created. You have to create them as a separate step. Java does NOT allow an array of Objects like C++ does.

– Peter Lawrey
Dec 17 '09 at 21:27












9 Answers
9






active

oldest

votes


















36














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.






share|improve this answer



















  • 5





    With your instantiation, you will have a 5-element array containing : {null, null, null, null, null}.

    – enguerran
    Dec 17 '09 at 15:53



















4














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();





share|improve this answer































    2














    i think by calling



     ResultList boll = new ResultList[5];


    you created a list which can hold 5 ResultList, i think you have to initialize boll[0] before you set value.



    boll[0] = new ResultList();





    share|improve this answer
























    • Mas 10 buen hombre ❤️❤️❤️

      – Marinha do Nascimento
      May 17 '18 at 14:27



















    2














    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";





    share|improve this answer

































      0














      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.






      share|improve this answer































        0














        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();
        }





        share|improve this answer

































          0














          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.






          share|improve this answer































            0














            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");
            }





            share|improve this answer

































              -1














              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() {}
              }





              share|improve this answer


























              • Either you can try the above 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

                – Hrishabkumar jha
                Nov 22 '17 at 7:23













              • Could you explain what you did to fix the problem?

                – Qwertie
                Nov 22 '17 at 7:39






              • 1





                This was already suggested in several answers.

                – shmosel
                Nov 22 '17 at 7:40











              • This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review

                – Mamun
                Nov 22 '17 at 8:25











              • @Mamun It does though.

                – shmosel
                Nov 22 '17 at 22:43


















              9 Answers
              9






              active

              oldest

              votes








              9 Answers
              9






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              36














              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.






              share|improve this answer



















              • 5





                With your instantiation, you will have a 5-element array containing : {null, null, null, null, null}.

                – enguerran
                Dec 17 '09 at 15:53
















              36














              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.






              share|improve this answer



















              • 5





                With your instantiation, you will have a 5-element array containing : {null, null, null, null, null}.

                – enguerran
                Dec 17 '09 at 15:53














              36












              36








              36







              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.






              share|improve this answer













              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.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Dec 17 '09 at 15:50









              Nathan HughesNathan Hughes

              73.1k16131211




              73.1k16131211








              • 5





                With your instantiation, you will have a 5-element array containing : {null, null, null, null, null}.

                – enguerran
                Dec 17 '09 at 15:53














              • 5





                With your instantiation, you will have a 5-element array containing : {null, null, null, null, null}.

                – enguerran
                Dec 17 '09 at 15:53








              5




              5





              With your instantiation, you will have a 5-element array containing : {null, null, null, null, null}.

              – enguerran
              Dec 17 '09 at 15:53





              With your instantiation, you will have a 5-element array containing : {null, null, null, null, null}.

              – enguerran
              Dec 17 '09 at 15:53













              4














              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();





              share|improve this answer




























                4














                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();





                share|improve this answer


























                  4












                  4








                  4







                  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();





                  share|improve this answer













                  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();






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Dec 17 '09 at 15:55









                  chburdchburd

                  3,6792332




                  3,6792332























                      2














                      i think by calling



                       ResultList boll = new ResultList[5];


                      you created a list which can hold 5 ResultList, i think you have to initialize boll[0] before you set value.



                      boll[0] = new ResultList();





                      share|improve this answer
























                      • Mas 10 buen hombre ❤️❤️❤️

                        – Marinha do Nascimento
                        May 17 '18 at 14:27
















                      2














                      i think by calling



                       ResultList boll = new ResultList[5];


                      you created a list which can hold 5 ResultList, i think you have to initialize boll[0] before you set value.



                      boll[0] = new ResultList();





                      share|improve this answer
























                      • Mas 10 buen hombre ❤️❤️❤️

                        – Marinha do Nascimento
                        May 17 '18 at 14:27














                      2












                      2








                      2







                      i think by calling



                       ResultList boll = new ResultList[5];


                      you created a list which can hold 5 ResultList, i think you have to initialize boll[0] before you set value.



                      boll[0] = new ResultList();





                      share|improve this answer













                      i think by calling



                       ResultList boll = new ResultList[5];


                      you created a list which can hold 5 ResultList, i think you have to initialize boll[0] before you set value.



                      boll[0] = new ResultList();






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Dec 17 '09 at 15:51









                      ufukgunufukgun

                      4,14062550




                      4,14062550













                      • Mas 10 buen hombre ❤️❤️❤️

                        – Marinha do Nascimento
                        May 17 '18 at 14:27



















                      • Mas 10 buen hombre ❤️❤️❤️

                        – Marinha do Nascimento
                        May 17 '18 at 14:27

















                      Mas 10 buen hombre ❤️❤️❤️

                      – Marinha do Nascimento
                      May 17 '18 at 14:27





                      Mas 10 buen hombre ❤️❤️❤️

                      – Marinha do Nascimento
                      May 17 '18 at 14:27











                      2














                      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";





                      share|improve this answer






























                        2














                        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";





                        share|improve this answer




























                          2












                          2








                          2







                          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";





                          share|improve this answer















                          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";






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Jan 22 '16 at 1:01









                          TylerH

                          15.7k105368




                          15.7k105368










                          answered Jan 7 '13 at 18:42









                          RishikeshDhokareRishikeshDhokare

                          2,3161427




                          2,3161427























                              0














                              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.






                              share|improve this answer




























                                0














                                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.






                                share|improve this answer


























                                  0












                                  0








                                  0







                                  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.






                                  share|improve this answer













                                  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.







                                  share|improve this answer












                                  share|improve this answer



                                  share|improve this answer










                                  answered Dec 4 '15 at 18:55









                                  spazBarnumspazBarnum

                                  462




                                  462























                                      0














                                      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();
                                      }





                                      share|improve this answer






























                                        0














                                        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();
                                        }





                                        share|improve this answer




























                                          0












                                          0








                                          0







                                          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();
                                          }





                                          share|improve this answer















                                          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();
                                          }






                                          share|improve this answer














                                          share|improve this answer



                                          share|improve this answer








                                          edited Jul 17 '17 at 22:04

























                                          answered Jul 4 '17 at 6:48









                                          Usama TahirUsama Tahir

                                          295




                                          295























                                              0














                                              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.






                                              share|improve this answer




























                                                0














                                                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.






                                                share|improve this answer


























                                                  0












                                                  0








                                                  0







                                                  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.






                                                  share|improve this answer













                                                  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.







                                                  share|improve this answer












                                                  share|improve this answer



                                                  share|improve this answer










                                                  answered Oct 28 '17 at 21:04









                                                  AnupAnup

                                                  113




                                                  113























                                                      0














                                                      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");
                                                      }





                                                      share|improve this answer






























                                                        0














                                                        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");
                                                        }





                                                        share|improve this answer




























                                                          0












                                                          0








                                                          0







                                                          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");
                                                          }





                                                          share|improve this answer















                                                          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");
                                                          }






                                                          share|improve this answer














                                                          share|improve this answer



                                                          share|improve this answer








                                                          edited Jan 1 '18 at 23:45









                                                          cricket_007

                                                          81.6k1143111




                                                          81.6k1143111










                                                          answered Jul 14 '17 at 14:05









                                                          arshadImamarshadImam

                                                          11




                                                          11























                                                              -1














                                                              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() {}
                                                              }





                                                              share|improve this answer


























                                                              • Either you can try the above 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

                                                                – Hrishabkumar jha
                                                                Nov 22 '17 at 7:23













                                                              • Could you explain what you did to fix the problem?

                                                                – Qwertie
                                                                Nov 22 '17 at 7:39






                                                              • 1





                                                                This was already suggested in several answers.

                                                                – shmosel
                                                                Nov 22 '17 at 7:40











                                                              • This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review

                                                                – Mamun
                                                                Nov 22 '17 at 8:25











                                                              • @Mamun It does though.

                                                                – shmosel
                                                                Nov 22 '17 at 22:43
















                                                              -1














                                                              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() {}
                                                              }





                                                              share|improve this answer


























                                                              • Either you can try the above 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

                                                                – Hrishabkumar jha
                                                                Nov 22 '17 at 7:23













                                                              • Could you explain what you did to fix the problem?

                                                                – Qwertie
                                                                Nov 22 '17 at 7:39






                                                              • 1





                                                                This was already suggested in several answers.

                                                                – shmosel
                                                                Nov 22 '17 at 7:40











                                                              • This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review

                                                                – Mamun
                                                                Nov 22 '17 at 8:25











                                                              • @Mamun It does though.

                                                                – shmosel
                                                                Nov 22 '17 at 22:43














                                                              -1












                                                              -1








                                                              -1







                                                              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() {}
                                                              }





                                                              share|improve this answer















                                                              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() {}
                                                              }






                                                              share|improve this answer














                                                              share|improve this answer



                                                              share|improve this answer








                                                              edited Jan 1 '18 at 23:45









                                                              cricket_007

                                                              81.6k1143111




                                                              81.6k1143111










                                                              answered Nov 22 '17 at 7:21









                                                              Hrishabkumar jhaHrishabkumar jha

                                                              93




                                                              93













                                                              • Either you can try the above 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

                                                                – Hrishabkumar jha
                                                                Nov 22 '17 at 7:23













                                                              • Could you explain what you did to fix the problem?

                                                                – Qwertie
                                                                Nov 22 '17 at 7:39






                                                              • 1





                                                                This was already suggested in several answers.

                                                                – shmosel
                                                                Nov 22 '17 at 7:40











                                                              • This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review

                                                                – Mamun
                                                                Nov 22 '17 at 8:25











                                                              • @Mamun It does though.

                                                                – shmosel
                                                                Nov 22 '17 at 22:43



















                                                              • Either you can try the above 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

                                                                – Hrishabkumar jha
                                                                Nov 22 '17 at 7:23













                                                              • Could you explain what you did to fix the problem?

                                                                – Qwertie
                                                                Nov 22 '17 at 7:39






                                                              • 1





                                                                This was already suggested in several answers.

                                                                – shmosel
                                                                Nov 22 '17 at 7:40











                                                              • This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review

                                                                – Mamun
                                                                Nov 22 '17 at 8:25











                                                              • @Mamun It does though.

                                                                – shmosel
                                                                Nov 22 '17 at 22:43

















                                                              Either you can try the above 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

                                                              – Hrishabkumar jha
                                                              Nov 22 '17 at 7:23







                                                              Either you can try the above 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

                                                              – Hrishabkumar jha
                                                              Nov 22 '17 at 7:23















                                                              Could you explain what you did to fix the problem?

                                                              – Qwertie
                                                              Nov 22 '17 at 7:39





                                                              Could you explain what you did to fix the problem?

                                                              – Qwertie
                                                              Nov 22 '17 at 7:39




                                                              1




                                                              1





                                                              This was already suggested in several answers.

                                                              – shmosel
                                                              Nov 22 '17 at 7:40





                                                              This was already suggested in several answers.

                                                              – shmosel
                                                              Nov 22 '17 at 7:40













                                                              This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review

                                                              – Mamun
                                                              Nov 22 '17 at 8:25





                                                              This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review

                                                              – Mamun
                                                              Nov 22 '17 at 8:25













                                                              @Mamun It does though.

                                                              – shmosel
                                                              Nov 22 '17 at 22:43





                                                              @Mamun It does though.

                                                              – shmosel
                                                              Nov 22 '17 at 22:43



                                                              Popular posts from this blog

                                                              Costa Masnaga

                                                              Fotorealismo

                                                              Sidney Franklin