Results 1 to 2 of 2
Thread: Exception Error
- 10-22-2010, 08:33 PM #1
Member
- Join Date
- Jun 2010
- Posts
- 5
- Rep Power
- 0
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 :
I know that some sentences are in greek but thats not a problem.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
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 09:22 PM.
- 10-22-2010, 10:14 PM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
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.Java Code:if(findPosition<table[mid].getPosition()) right=mid-1; else if(findPosition>table[mid].getPosition()) left=mid+1;
Similar Threads
-
exception error
By kira137 in forum New To JavaReplies: 5Last Post: 10-12-2009, 07:46 AM -
Exception error
By Rose88 in forum New To JavaReplies: 8Last Post: 07-06-2009, 10:22 PM -
Exception error
By jaiminparikh in forum New To JavaReplies: 0Last Post: 03-20-2009, 09:06 PM -
JSF error+exception
By Peter in forum SWT / JFaceReplies: 1Last Post: 07-04-2007, 06:29 AM -
Difference between error and exception
By harinath chakrapani in forum New To JavaReplies: 1Last Post: 06-19-2007, 08:49 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks