继续获取空指针异常错误?我的逻辑几乎准确

时间:2014-05-16 21:54:18

标签: java class exception-handling nullpointerexception try-catch

为什么当我输入1或2来激活开关盒1或2时我有一个空指针异常错误?我真的需要能够解决问题的人。我相信我的开关盒没有问题。我错过了什么?

   package dakilangtaoako;
   import java.io.BufferedReader;
   import java.io.IOException;
   import java.io.InputStreamReader;
    import java.util.logging.Level;
   import java.util.logging.Logger;


 public class DakilangTaoAko {
public static void main(String[] args){

    BufferedReader dataIn = new BufferedReader(new InputStreamReader( System.in) );
    String mydata[][] = new String[5][3] ;
    String inttoword [] = new String [5];
    int wordtoint[] = new int [5]; 
    int mychoice = 0;
    String toreturn = "";

    for (int outside = 0; outside < 5; outside++){
        System.out.println("Entry no." + (outside + 1));
        for (int inside = 0; inside < 3; inside++){
        if(inside == 0){
            try {
                System.out.print("Name: ");
                mydata[outside][inside]= dataIn.readLine();
            } catch (IOException ex) {
                Logger.getLogger(DakilangTaoAko.class.getName()).log(Level.parse("OK"), null, ex);
            }
 }
        else if(inside == 1){
            try {
                System.out.print("ID: ");
                mydata[outside][inside]= dataIn.readLine();
            } catch (IOException ex) {
                Logger.getLogger(DakilangTaoAko.class.getName()).log(Level.parse("OK"), null, ex);
            }
  }
        else if(inside ==2){
            try {
                System.out.print("Subject: ");
                mydata[outside][inside]= dataIn.readLine();
            } catch (IOException ex) {
                Logger.getLogger(DakilangTaoAko.class.getName()).log(Level.parse("OK"), null, ex);
            }


 }
}
    }
 for(int outside= 0; outside <5; outside++){
            for(int inside = 1; inside <= 3; inside++){
                if(inside ==0)
                    inttoword[outside]=(mydata[outside][inside])+";"+(mydata[outside][inside]);
            }
       }
 for(int outside = 0; outside < 5; outside++){
            for(int inside = 1; inside <= 3; inside++){
                if(inside ==0)
                    wordtoint[outside]=Integer.parseInt(mydata[outside][inside]);
            }
       }


 for(int i=0;i<5;i++){
 wordtoint[i]=Integer.parseInt(mydata[i][1]);
 }

       while(mychoice != 5){
       System.out.println("-------- DISPLAY OPTIONS --------");
       System.out.println("\t [1] Display Entries Alphabetically ");
       System.out.println("\t [2] Display Entries by Ascending ID number ");
       System.out.println("\t [3] Display Students Enrolled in Prog2 ");
       System.out.println("\t [4] Display Students Enrolled in CSO ");
       System.out.println("\t [5] Exit ");
       System.out.println();
       System.out.print("\t Choice:");
            try {
                mychoice = Integer.parseInt(dataIn.readLine());
            } catch (IOException ex) {
                Logger.getLogger(DakilangTaoAko.class.getName()).log(Level.SEVERE, null, ex);
            }

            switch (mychoice){



                case 1:
                  System.out.println("----Entries In Alphabetical Order----");
                  alphabetSorter(inttoword);
                        for(int limiter = 0; limiter < 5; limiter++)
               System.out.println(inttoword[limiter]);


                  System.out.print("Press [r] to Return to Main Menu...");
        try {
           toreturn = dataIn.readLine();
       } catch (IOException ex) {
           Logger.getLogger(DakilangTaoAko.class.getName()).log(Level.SEVERE, null, ex);
       }
                     if("r".equals(toreturn)) 
                    break;
                case 2:

                  System.out.println(" ---- Entries by Ascending ID Number ----");  
                        for(int outside = 0; outside < 5; outside++){
                        numSorter(wordtoint);
                        for(int inside = 0; inside < 5; inside++){
                        if(Integer.parseInt(mydata[inside][1])== wordtoint[outside])
                  System.out.println(wordtoint[outside] + ";" + mydata[inside][0]);
               }                
           } 
                   System.out.print("Press [r] to return to main Menu...");
        try {
           toreturn = dataIn.readLine();
       } catch (IOException ex) {
           Logger.getLogger(DakilangTaoAko.class.getName()).log(Level.SEVERE, null, ex);
       }            
                 if("r".equals(toreturn))
                         break;

                case 3:
                    System.out.println(" ---- Students Enrolled in Prog2 ----");
                    String mustfind1 = "PROG2";     
                    for(int outside = 0; outside < 5; outside++){
                    if(mydata[outside][2].equalsIgnoreCase(mustfind1)){
                    System.out.println(mydata[outside][0]+";"+mydata[outside][1]);
                                        }
                        }System.out.print("Press [r] to Return to Main Menu...");
        try {
           toreturn = dataIn.readLine();
       } catch (IOException ex) {
           Logger.getLogger(DakilangTaoAko.class.getName()).log(Level.SEVERE, null, ex);
       }
                  if("r".equals(toreturn))   
                    break;

                case 4:
                    System.out.println(" ---- Students Enrolled in CSO ---- ");
                    String mustfind2 = "CSO";
                    for(int outside = 0; outside < 5; outside++){
                        if(mydata[outside][2].equalsIgnoreCase(mustfind2))
                            System.out.println(mydata[outside][0]+";"+mydata[outside][1]);
                                        }
                        System.out.print("Press [r] to Return to Main Menu...");
       try {
           toreturn = dataIn.readLine();
       } catch (IOException ex) {
           Logger.getLogger(DakilangTaoAko.class.getName()).log(Level.SEVERE, null, ex);
       }
                  if("r".equals(toreturn))
                    break;

                case 5:
                    mychoice = 5;

                    break;  

                default:
                    System.out.println("Sorry the Number is Not in the Choices!!");
                    System.out.print("Press [r] to Return to Main Menu...");
        try {
           toreturn = dataIn.readLine();
             } catch (IOException ex) {
           Logger.getLogger(DakilangTaoAko.class.getName()).log(Level.SEVERE, null, ex);
          }
                 if("r".equals(toreturn))
                     break;
            }     }   
   }
       public static void numSorter(int given[]){

  int temp;
  for(int outside = 0; outside < given.length-1; outside++)
    {
        for(int inside = outside+1 ; inside < given.length; inside++)
        {  
            if(given[outside] > given[inside])
            {                                         
            temp = given[outside];
            given[outside]= given[inside];  
            given[inside]=temp;                                    
            } 
        } 
    } 
    }
    public static void alphabetSorter(String given[]){

String temp;
for(int outside = 0; outside < given.length -1; outside++)
    {
        for(int inside = outside +1; inside < given.length; inside++)
        {  
            if(given[outside].compareToIgnoreCase(given[inside])>0)
            {                                         
            temp= given[outside];
            given[outside]= given[inside];  
            given[inside]=temp;                                    
            } 
        } 
      } 
 }




  }

1 个答案:

答案 0 :(得分:0)

这是因为你没有初始化数组inttoword。它的所有条目都为空,这会使这句话窒息:

if (given[outside].compareToIgnoreCase(given[inside]) > 0)

你要求jdk这样做: if (null.compareToIgnoreCase(null) > 0)