hello m new here starting java can anyone guide me how ti initilize array of objects in java em receving java.lang.NULL pointer exception

choice=Integer.parseInt(JOptionPane.showInputDialog(null,"Enter choice"));

       Patient pati[]=new Patient[49];
   if(choice == 1)
   {



    pati[i].setdata();
    pati[i].showdata();
    count++;
    }
    if(choice== 2)
    {

    pati[count].update();
    pati[count].showup();       
    }


    }while(choice!=3);






}

Edited 3 Years Ago by Nick Evan: Fixed formatting

pati.setdata();

since pati is still a null value, you'll have to begin with:
pati = new Patient(); // or other constructor, anyway, you can't (shouldn't) call methods on a non-existing instance of an object.

this is how you initiliaze only one objects i want to initlize 50 objects of Patient class
error shows main.java.lang Null.pointer exception when i used Patient pati[]=new Patient[49]

here is code how can i initiliaze 50 objects

package pat.OOP;
import javax.swing.JOptionPane;
public class Client
{
    public static void main(String [] args)
    {

       int count=0;
       int choice=0;
       int i=0;
            do
            {


       choice=Integer.parseInt(JOptionPane.showInputDialog(null,"Enter choice"));
           Patient pati[]=new Patient[49];
       if(choice == 1)
       {



        pati[i].setdata();
        pati[i].showdata();
        count++;
        }
        if(choice== 2)
        {

        pati[count].update();
        pati[count].showup();       
        }


        }while(choice!=3);






    }

}

Edited 3 Years Ago by Dani: Formatting fixed

here is the code i want to iniliaze 50 objects using array; how can i initilize
package pat.OOP;

import javax.swing.JOptionPane;
public class Client
{
    public static void main(String [] args)
    {

       int count=0;
       int choice=0;
       int i=0;
            do
            {


       choice=Integer.parseInt(JOptionPane.showInputDialog(null,"Enter choice"));
           Patient pati[]=new Patient[49];
       if(choice == 1)
       {



        pati[i].setdata();
        pati[i].showdata();
        count++;
        }
        if(choice== 2)
        {

        pati[count].update();
        pati[count].showup();       
        }


        }while(choice!=3);






    }

}

Edited 3 Years Ago by Nick Evan: Fixed formatting

yes, it happens one by one.
but sure, you can add a for loop and do it something like:

Patient[] pati = new Patient[49];

for ( int i = 0; i < pati.length; i++ ){
pati = new Patient();
}

Have you guys forgotten about something?? The code tags!!???

you think the world will come to an end because of me posting three lines of code not between code-tags? (Damn, those maya's were right after all :-|)


yes, code tags makes the whole a lot more easier to read, but in this case, every code posted was pretty straight-forward and easy to read, even while not in code tags, so I don't think it's a capital crime.

no one can or will blame you if you decide not to help solving the problem here (which probably has been solved, even without the use of code-tags), no one will even look badly at you for asking for code-tags, but what's stopping you from formulating it a bit like this:

"Hey, could you guys please use code tags? It's a bit hard to read."

you think the world will come to an end because of me posting three lines of code not between code-tags? (Damn, those maya's were right after all :-|)


yes, code tags makes the whole a lot more easier to read, but in this case, every code posted was pretty straight-forward and easy to read, even while not in code tags, so I don't think it's a capital crime.

no one can or will blame you if you decide not to help solving the problem here (which probably has been solved, even without the use of code-tags), no one will even look badly at you for asking for code-tags, but what's stopping you from formulating it a bit like this:

"Hey, could you guys please use code tags? It's a bit hard to read."

rules and regulations.... They were made to assist others and ourselves. its ok not to put tags but its not right for the ake of the forum's regulations.... wait a munite. never mind.

This article has been dead for over six months. Start a new discussion instead.