我的程序有问题,它编译并运行但是当我首先进入案例时,它会按照案例说明然后停止并给我这样的消息:
"异常int thread" main" testVessel.main上的java.lang.NullPointerException(testVessel.java:165)
因为我认为问题是来自第一个开关的情况3。 这是主要的课程:
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;
}
}
}
以下是次要的:
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;
}
}
答案 0 :(得分:1)
此数组的元素为null
。
Vessel[] skafoi=new Vessel[10];
确保首先通过
初始化它们
调用适当的构造函数。
当你到达这条线时:
System.out.println(skafoi[i].EmfanishOlwn());
并非所有这些都被初始化,所以你得到了
NullPointerException
。