Results 1 to 2 of 2

Thread: Exception Error

  1. #1
    Sotsiak is offline Member
    Join Date
    Jun 2010
    Posts
    5
    Rep Power
    0

    Default Exception Error

    Hello hello, I write a java program for my university, right know I havent completed it yet but it runs ok.
    First of all here is the code :

    Java Code:
    import java.io.*;
    import java.util.Calendar;
    import java.text.SimpleDateFormat;
    
    class UserInput {
     public static String getString() {
      String line;
      InputStreamReader input=new InputStreamReader(System.in);
      BufferedReader in=new BufferedReader(input);
      try {
       line=in.readLine();
       return line;
      }
      catch(Exception e) {
      return "Exception";
      }
     }
    
     public static int getInteger() {
      String line;
       InputStreamReader input=new InputStreamReader(System.in);
       BufferedReader in=new BufferedReader(input);
     try {
      line=in.readLine();
      int i=Integer.parseInt(line);
      return i;
     }
     catch(Exception e) {
     return -1;
      }
     }
    }
    
    class Vehicle{
        private int number;
        private String sign;
        private String type;
        private String time;
        private int position;
    
        public Vehicle(int number, String sign, String type, String time, int position){
            this.number=number;
            this.sign=sign;
            this.type=type;
            this.time=time;
            this.position=position;
        }
    
        public Vehicle(){
            number=-1;
            sign=" ";
            type=" ";
            time=" ";
            position=-1;
        }
    
        public void setNumber(int n){
            number=n;
        }
    
        public void setSign(String s){
            sign=s;
        }
    
        public void setType(String t){
            type=t;
        }
    
        public void setTime(String t){
            time=t;
        }
    
        public void setPosition(int p){
            position=p;
        }
    
        public int getNumber(){
            return number;
        }
    
        public String getSign(){
            return sign;
        }
    
        public String getType(){
            return type;
        }
    
        public String getTime(){
            return time;
        }
    
        public int getPosition(){
            return position;
        }
    }
    
    class MyUtils{
        public static final String DATE_FORMAT_NOW = "HH:mm:ss";
        public static String findTime(){
            Calendar cal = Calendar.getInstance();
            SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
            return sdf.format(cal.getTime());
        }
    
        public static int seq_search(Vehicle table[], String findSign){
            for(int i=0; i<table.length; i++){
                if(table[i].getSign().equals(findSign))
                    return i;
            }
            return -1;
        }
    
        public static int bin_search(Vehicle table[], int findPosition){
            int left=0;
            int right=table.length-1;
            int mid;
            int pos=-1;
    
            while(pos==-1&&left<=right){
                mid=(left+right)/2;
                [B]if(findPosition<table[mid].getPosition())[/B]
                    right=mid-1;
                else if(findPosition>table[mid].getPosition())
                    left=mid+1;
                else pos=mid;
            }
            return pos;
        }
    
        public static int protropi(boolean table[]){
            for(int i=0; i<table.length; i++){
                if(table[i]==false)
                    return i;
            }
            return -1;
        }
    
        public static void printVehicle(int i, Vehicle table[]){
            System.out.println("Aukson Arithmos: "+table[i].getNumber());
            System.out.println("Pinakida: "+table[i].getSign());
            System.out.println("Typos: "+table[i].getType());
            System.out.println("Hmeromhnia: "+table[i].getTime());
            System.out.println("Thesi: "+table[i].getPosition()+1);
        }
    }
    
    public class VehicleDemo{
        public static void main(String args[]) throws IOException{
    
            int choice;
            int counter=1;
            int number;
            String sign;
            String type;
            String time;
            int position;
    
            Vehicle table[]=new Vehicle[10];
            boolean theseis[]=new boolean[10];
            
            for(int i=0; i<theseis.length; i++)
                theseis[i]=false;
    
            boolean menu=false;
            do{
    
            System.out.println("      Arxiko menu");
            System.out.println("1. Proselefsi Oximatos");
            System.out.println("2. Emfanisi Stixion Oximatos:");
            System.out.println("3. Lista Theseon Parking");
            System.out.println("4. Telos");
            System.out.print("      Doste epilogi (1-4): ");
    
    
            choice=UserInput.getInteger();
    
                menu:{
            if(choice==1){
                boolean flag=false;
    
                for(int i=0; i<theseis.length; i++)
                    if(theseis[i]==false)
                        flag=true;
    
                if(flag==false)
                    System.out.println("Den uparxei keni thesi");
                    
    
                if(flag==true){
                    System.out.print("Sas protinoume autin tin thesi: ");
    
                    int protinomeno_pos;
                    protinomeno_pos=MyUtils.protropi(theseis);
    
                    System.out.println(protinomeno_pos+1);
    
                System.out.println();
    
                number=counter; 
    
                System.out.println("Dose arithmo pinakidas: ");
                sign=UserInput.getString();
    
                System.out.println("Dose ton typo tou autokinitou: ");
                type=UserInput.getString();
    
                time=MyUtils.findTime();
    
                System.out.println("Dose thn thesi parking");
                position=UserInput.getInteger();
                position--;
                if(position!=protinomeno_pos)
                    if(theseis[position]==true){
                        System.out.println("H thesi den einai keni");
                        break menu;
                    }
    
                if(position>10){
                    System.out.println("Lathos arithmos thesis");
                    break menu;
                }
                table[position]=new Vehicle(number, sign, type, time, position);
    
                theseis[position]=true;
    
                counter++;
                }
    
            }
                }
            if(choice==2){
                System.out.println("Dialekse tropo anazitisis");
                System.out.println("1. Me bash thn pinakida");
                System.out.println("2. Me bash ton arithmo thesis");
    
                int choose;
                choose=UserInput.getInteger();
    
                if(choose==1){
                    System.out.println("Dose ton arithmo ths pinakidas");
                    String findSign;
                    findSign=UserInput.getString();
                    int done;
                    done=MyUtils.seq_search(table, findSign);
    
                    if(done==-1)
                        System.out.println("To oxima den brethike");
                    else{
                        System.out.println("To oxima brethike");
                        MyUtils.printVehicle(done, table);
                    }
                }
    
                if(choose==2){
                    System.out.println("Dose ton arithmo thesis");
                    int findPosition;
                    findPosition=UserInput.getInteger();
                    findPosition--;
                    int done;
                    [B]done=MyUtils.bin_search(table, findPosition);[/B]
    
                    if(done==-1)
                        System.out.println("To oxima den brethike");
                    else{
                        System.out.println("To oxima brethike");
                        MyUtils.printVehicle(done, table);
                    }
                }
                
    
            }
    
            if(choice==3){
                System.out.println("Lista theseon parking:");
                for(int i=0; i<theseis.length; i++){
                    if(theseis[i]==true)
                        System.out.println("Thesi "+i+": Kateillhmeni me arithmo pinakidas autokinitou "+table[i].getSign());
    
                    if(theseis[i]==false)
                        System.out.println("Thesi "+i+": Diathesimi");
                }
                
            }
    
            if(choice==4){
                System.out.println("Telos programmatos");
                menu=true;
                }
            }while(menu==false);
    
        }
    }
    
    
    // Ekremei to ESC function
    I know that some sentences are in greek but thats not a problem.

    The error i get is this:
    Exception in thread "main" java.lang.NullPointerException
    at MyUtils.bin_search(VehicleDemo.java:122)
    at VehicleDemo.main(VehicleDemo.java:261)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 15 seconds)

    I bolded the two lines of code. When I give the position of the Vehicle I want to search, it gives me this error message. Because something is going wrong at bin_search().

    I would appreciate if you could help me. I use seq_search() to search for a certain Vehicle with a certain position.

    Thanks.

    EDIT:
    Ok, I think I know the problem. It happens with other methods too. Is it because I didnt fill all the Vehicle table[] ??
    What can I do to prevent this error to happen, maybe use another constructor?

    Thanks.
    Last edited by Sotsiak; 10-22-2010 at 10:22 PM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Java Code:
    if(findPosition<table[mid].getPosition())
        right=mid-1;
    else if(findPosition>table[mid].getPosition())
        left=mid+1;
    Yes - that logic is going to blow up if table[mid] is null. The easiest thing I can think of to fix this is ... not to use an array. If you use a List of some sort like ArrayList<Vehicle> then there are already library methods to search and sort.

Similar Threads

  1. exception error
    By kira137 in forum New To Java
    Replies: 5
    Last Post: 10-12-2009, 08:46 AM
  2. Exception error
    By Rose88 in forum New To Java
    Replies: 8
    Last Post: 07-06-2009, 11:22 PM
  3. Exception error
    By jaiminparikh in forum New To Java
    Replies: 0
    Last Post: 03-20-2009, 10:06 PM
  4. JSF error+exception
    By Peter in forum SWT / JFace
    Replies: 1
    Last Post: 07-04-2007, 07:29 AM
  5. Difference between error and exception
    By harinath chakrapani in forum New To Java
    Replies: 1
    Last Post: 06-19-2007, 09:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •