program run but stopped and and shows java.lang.nullpointerexception -


i have problem program compiles , run when go case first case says stop , give me messages "exception int thread "main" java.lang.nullpointerexception @ testvessel.main(testvessel.java:165) think problem int case 3 first switch. here main class:

    class testvessel     {     public static void main(string args[])     {          string name,idioktname,skafos,temp1;         double length,width,ektopisma,maxspeed,max,ektop;         int etos,epilogh,size=0,thesi,i=0,o,a,year;         boolean shmaia=true,flag;         vessel[] skafoi=new vessel[10];         for(;shmaia;)            {                 system.out.println("lista epilogwn");                system.out.println("1.eisagwgh stoixeiwn skafous");                system.out.println("2.emfanish stoixeiwn skafous");                system.out.println("3.emfanish oloklhrou tou pinaka");                system.out.println("4.telos");                system.out.println("dwste epilogh(1-4):");                epilogh=userinput.getinteger();                if(epilogh==4)shmaia=true;                 switch(epilogh)                {                      case 1 :                            system.out.println("dwste onoma skafous:");                            name=userinput.getstring();                            system.out.println("dwste mhkos:");                             flag=true;                            length=0;                            for(;flag;)                               {                                  length=userinput.getdouble();                                  if(length>=0 && length<=1000)flag=false;                                  else                                      system.out.println("mh apodekta oria xanadokimaste\n");                               }                                                           system.out.println("dwste platos:");                            width=userinput.getdouble();                            system.out.println("dwste ektopisma:");                            flag=true;                            ektopisma=0;                            for(;flag;)                               {                                  ektopisma=userinput.getdouble();                                  if(ektopisma>=0 && ektopisma<=150000)flag=false;                                  else                                      system.out.println("mh apodekta oria xanadokimaste\n");                               }                            system.out.println("dwste etos kataskeuhs:");                            etos=userinput.getinteger();                            system.out.println("dwste megisth taxhtyta:");                            maxspeed=userinput.getdouble();                            system.out.println("dwste onoma idioktith:");                            idioktname=userinput.getstring();                            skafoi[i]=new vessel(name,length,width,ektopisma,etos,maxspeed,idioktname);                            i++;                            size++;                            break;                     case 2 :                            if(size==0)system.out.println("den uparxoun skafoi sth lista xanadokismaste \n");                            if(size!=0)                            {system.out.println("epilogh pediou anazhthshs");                            system.out.println("1.onoma skafous");                            system.out.println("2.etos kataskeuhs");                             system.out.println("3.ektopisma");                            system.out.println("4.megisto mhkos skafous");                            system.out.println("5.epistrodh sthn arxikh lista epilogwn");                            system.out.println("dwste epilogh(1-5):");                            epilogh=userinput.getinteger();                            switch(epilogh)                            {                                 case 1:                                        myutils.epiloghmethodou();                                        epilogh=userinput.getinteger();                                        switch(epilogh)                                        {                                             case 1:                                                    system.out.println("dwste onoma skafous");                                                    skafos=userinput.getstring();                                                    thesi=myutils.seqsearch(skafoi,skafos);                                                    system.out.println(skafoi[thesi].emfanisholwn());                                                    break;                                             case 2:                                                    system.out.println("dwste onoma skafous");                                                    skafos=userinput.getstring();                            thesi=myutils.binsearch(skafoi,skafos,size);                                                    system.out.println(skafoi[thesi].emfanisholwn());                                                    break;                                             case 3:                                                    break;                                             default: system.out.print("\n\tlathos epilogi methodou ... eisagete ari8mo 1-3\n");                                        }break;                                  case 2:                                        myutils.epiloghmethodou();                                        epilogh=userinput.getinteger();                                        switch(epilogh)                                        {                                            case 1:                                                    system.out.println("dwste etos kataskeuhs");                                                    year=userinput.getinteger();                                                    myutils.seqsearch(skafoi,year,size);                                                    break;                                            case 2:                                                    system.out.println("dwste etos kataskeuhs");                                                    year=userinput.getinteger();                                                    myutils.binsearch(skafoi,year,size);                                                   break;                                            case 3:                                                    break;                                            default: system.out.print("\n\tlathos epilogi methodou ... eisagete ari8mo 1-3\n");                                         }break;                                  case 3:                                        myutils.epiloghmethodou();                                        epilogh=userinput.getinteger();                                        switch(epilogh)                                        {                                            case 1:                                                    system.out.println("dwste ektopisma");                                                    ektop=userinput.getdouble();                                                    myutils.seqsearch(skafoi,ektop,size);                                                    break;                                            case 2:                                                    system.out.println("dwste ektopisma");                                                    ektop=userinput.getdouble();                                                    myutils.binsearch(skafoi,ektop,size);                                                    break;                                            case 3:                                                    break;                                            default: system.out.print("\n\tlathos epilogi methodou ... eisagete ari8mo 1-3\n");                                        }break;                                  case 4:                                         myutils.epiloghmethodou();                                        epilogh=userinput.getinteger();                                        max=0;                                        a=0;                                        switch(epilogh)                                        {                                            case 1:                                                    myutils.seqsearch(skafoi,max,size,a);                                                    system.out.println("h max taxhthta einai:"+max);                                                    break;                                             case 2:                                                    myutils.binsearch(skafoi,max,size);                                                    system.out.println("h max taxhthta einai:"+max);                                                    break;                                            case 3:                                                    break;                                            default: system.out.print("\n\tlathos epilogi methodou ... eisagete ari8mo 1-3\n");                                        }                                  case 5:                                        break;                                        default: system.out.print("\n\tlathos epilogi methodou ... eisagete ari8mo 1-5\n");}                              }                              break;                       case 3:                             if(size!=0)                                {for(i=0;i<skafoi.length;i++)                                   {                                       system.out.println(skafoi[i].emfanisholwn());                                   }                                }                              if(size==0)                                  {system.out.println();                                   system.out.println("den uparxoun skafoi sth lista");                                   system.out.println();}                               break;                     case 4:                               system.out.println("telos programmatos");                               shmaia=false;                               break;                               default: system.out.print("\n\tlathos epilogi methodou ...  eisagete ari8mo 1-4\n");        } if(size==10)shmaia=false;  }  }  }   

and here secondary: class myutils {

 public static  void seqsearch(vessel[] a,int key,int size)  {       int x=0;       for(int i=0;i<size;i++)         if(a[i].getetos()==key)           {system.out.println(a[i].emfanisholwn());            x++;}         if(x==0)system.out.println("den uparxoun skafoi autou tou etous");  }  public static int seqsearch(vessel[] a,string key)  {       for(int i=0;i<a.length;i++)         if(a[i].getname()==key)            return i;       return -1;  }  public static void seqsearch(vessel[] a,double key,int size)  {       int x=0;       for(int i=0;i<size;i++)          if(a[i].getektopisma()==key)             {system.out.println(a[i].emfanisholwn());             x++;}          if(x==0)system.out.println("den uparxoun skafoi me tetoio ektopisma");  }  public static void seqsearch(vessel[] a,double max,int size,int l)  {           l=0;          max=0;          for(int i=0;i<size;i++)            if(a[i].getmaxspeed()>max)max=a[i].getmaxspeed();   }  public static int binsearch(vessel[] a,string key,int size)  {        int left=0,right=size-1;        return binarysearch(a,key,left,right);  }   private static int binarysearch(vessel[] numbers,string key,int left,int right)  {       int mid,pos=-1;       while(pos==-1 && left<=right)        {            mid=(left+right)/2;            if(numbers[mid].getname().compareto(key)>0)              right=mid-1;            else              if(numbers[mid].getname().compareto(key)<0)                left=mid+1;              else                pos=mid;        }       return pos;   }  public static void binsearch(vessel[] a,int key,int size)  {          int left=0,right=size-1;       for(int i=0;i<size;i++)           if(a[i].getetos()==a[binarysearch(a,key,left,right)].getetos())system.out.println(a[i].emfanisholwn());  }  private static int binarysearch(vessel[] numbers,int key,int left,int right)  {       int mid,pos=-1;       while(pos==-1 && left<=right)        {            mid=(left+right)/2;            if(key<numbers[mid].getetos())              right=mid-1;            else              if(key>numbers[mid].getetos())                left=mid+1;              else                pos=mid;        }       return pos;   }   public static void binsearch(vessel[] a,double key,int size)   {         int left=0,right=size-1;        for(int i=0;i<size;i++)           if(a[i].getektopisma()==a[binarysearch(a,key,left,right)].getektopisma())system.out.println(a[i].emfanisholwn());   }                  private static int binarysearch(vessel[] numbers,double key,int left,int right)   {       int mid,pos=-1;       while(pos==-1 && left<=right)        {            mid=(left+right)/2;            if(key<numbers[mid].getektopisma())              right=mid-1;            else              if(key>numbers[mid].getektopisma())                left=mid+1;              else                pos=mid;        }       return pos;    }     public static void epiloghmethodou()  {     system.out.println("epilogh methodou anazhthshs");      system.out.println("1.seiriakh anazhthsh");     system.out.println("2.duadiki anazhthsh");     system.out.println("3.epistrofh sthn epilogh pediou anazhthshs");     system.out.println("dwste epilogh(1-3):");   }  }     class vessel { private string name; private double length; private double width; private double ektopisma; private int etos; private double maxspeed; private string idiokname; vessel(string n,double l,double w,double e,int et,double ms,string cn) {     name=n;     length=l;     width=w;     ektopisma=e;     etos=et;     maxspeed=ms;     idiokname=cn; } public string getname() {     return name; } public double getlength() {      return length; } public double getwidth() {     return width; } public double getektopisma() {     return ektopisma; } public int getetos() {     return etos; } public double getmaxspeed() {     return maxspeed; } public string getidiokname() {     return idiokname; } public string emfanisholwn() {     string tostring=("to onoma einai: "+name+"\n");     tostring+=("to mhkos einai: "+length+"\n");     tostring+=("to platos einai: "+width+"\n");     tostring+=("to ektopisma einai: "+ektopisma+"\n");     tostring+=("to etos einai: "+etos+"\n");     tostring+=("h megisth taxhtyta einai: "+maxspeed+"\n");     tostring+=("to onoma tou idiokthth einai: "+idiokname+"\n");     return tostring;   }   }   import java.util.scanner;  class userinput  {  public static int getinteger()  {  scanner input = new scanner(system.in);  try  {  int x;  x = input.nextint();  return x;  }  catch (exception e )  {  system.out.println("error");  return -1;  }  }  public static double getdouble()  {  scanner input = new scanner(system.in);  try{  double x;  x = input.nextdouble();  return x;  }  catch(exception e)  {  system.out.println("error");  return -1;  }  }  public static float getfloat()  {  scanner input = new scanner(system.in);  try{  float x;  x = input.nextfloat();  return x;  }  catch (exception e){  system.out.println("error");  return -1;  }  }  public static string getstring()  {  scanner input = new scanner(system.in);  string x;  x = input.nextline();  return x;  }  } 

the elements of array null.

vessel[] skafoi=new vessel[10];

make sure initialize them first by
calling appropriate constructor.

when line:
system.out.println(skafoi[i].emfanisholwn());
not of them initialized get
nullpointerexception.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -