Hello everybody.... I'm new in your foroum and i see that you help everybodys problems so i'd too like to get some help about my problem in my java programm....
I have to make a programm that will read one men and if the answer of the user is 1 he is gonna put in the array the name,the singer,the kind and the position of the song in one top 10 (in our case it's gonna be top 2 so we won't have many variables to input....
When the user gives the command 1 in the next menu he has to search about an artist in the Top10 and print the position of the singer in the Top10!!!! When the user gives the command 2 in the menu he gives the position that want's to know which song is there.... And here is my problem... even if my programm don't have any errors,when i fill the Top10 (I said again top2 in our case!) and search about the artist or the position it throws me a strange error.... This : <<< Exception in thread "main" java.lang.NullPointerException
at MyUtils.search(TestSong.java:165)
at MyUtils.bin_search(TestSong.java:159)
at menu.gemisma(TestSong.java:117)
at menu.gemisma(TestSong.java:93)
at TestSong.main(TestSong.java:54) >>>
Here is my code and I hope you gonna help me!!!!
p.s 1 The System.out.println's and the menu are in greek so please be patient.... I tried to explain you the way that the programm's work,but if there are any questions please ask!
p.s 2 GREETINGS FROM GREECE!
import java.io.*; class UserInput { //Class gia eisagogi dedomenwn apo to pliktrologio public static String getString() { //Methodos gia eisagogi String 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() { //Methodos gia eisagogi Integer 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 Song { String titlos; String kalitexnis; String eidos_tragoudiou; int position; Song (String title,String name,String eidos,int pos) { titlos=title; kalitexnis=name; eidos_tragoudiou=eidos; position=pos; } } class TestSong { public static void main(String args[]) { Song TopTen[] = new Song[10]; int i=0; int a_a = 0; menu.gemisma(i,TopTen); } } class menu { static void gemisma(int i,Song TopTen[]) { String esc="esc"; System.out.println(" Αρχικό Μενού "); System.out.println("1.Προσθήκη τραγουδιού"); System.out.println("2.Εμφάνιση Στοιχείων Τραγουδιού"); System.out.println("3.Λίστα Top-Ten"); System.out.println("4.Έξοδος"); System.out.println("∆ώστε Επιλογή (1-4) : "); int epilogi_xristi = UserInput.getInteger(); if (i<1 && epilogi_xristi==1) { System.out.println("Dwse to tragoudi : "); String titlos = UserInput.getString(); if (titlos.equals("esc")) { menu.gemisma(i,TopTen); } System.out.println("Dwse ton kalitexni tou tragoudiou :"); String kalitexnis = UserInput.getString(); if (kalitexnis.equals("esc")) { menu.gemisma(i,TopTen); } System.out.println("Dwse to idos tou tragoudiou :"); String eidos_tragoudiou = UserInput.getString(); if (eidos_tragoudiou.equals("esc")) { menu.gemisma(i,TopTen); } System.out.println("Dwse th thesh tou tragoudiou sti dekada :"); int position = UserInput.getInteger(); TopTen[i] = new Song(titlos,kalitexnis,eidos_tragoudiou,position); menu.gemisma(i+1,TopTen); } if (epilogi_xristi==2) System.out.println("Ama 8eleis h anazhthsh na ginei me vash ton Kalitexni pata 1,ama 8es na ginei me vash th 8esh sth dekada pata 2 !!"); int epil = UserInput.getInteger(); if (epil==1) //Seiriakh anazhthsh me vash ton kalitexni { System.out.println("Dwse to onoma tou kalitexni pou psaxneis"); String key1 = UserInput.getString(); int ret_key1 = MyUtils.seq_search(key1,TopTen); if (ret_key1!=-1) { System.out.println("O kalitexnis pou edwses den htan mesa sto TopTen"); menu.gemisma(i,TopTen); } else { System.out.println("O kalitexnis pou edwses einai mesa sto TopTen sth thesh :" + TopTen[ret_key1].position); } } else if (epil==2) //diadiki anazitisi me vash th 8esh sth dekada { System.out.println("Dwse th thsi sth dekada sthn opoia thes na deis to tragoudi pou uparxei ekei!! "); int key2 = UserInput.getInteger(); int ret_key2 = MyUtils.bin_search(key2,TopTen); if (ret_key2!=-1) { System.out.println("Sth thesi pou zitises vrisketai to tragoudi : " + TopTen[ret_key2].titlos); } else { System.out.println("Den edwses kapoia thesi apo ton arithmo 1 mexri to 10"); } } else { System.out.println("Edwses lathos epilogh!!"); menu.gemisma(i,TopTen); } if (i==1) { System.out.println("***Exei gemisei o pinakas***"); menu.gemisma(i,TopTen); } } } class MyUtils { public static int seq_search(String key1,Song TopTen[]) { int z=-1; for (int i=0; i<TopTen.length; i++) if (TopTen[i].kalitexnis.equals(key1)) { z=i; } return z; } public static int bin_search(int key2,Song TopTen[]) { int left =0; int right = TopTen.length-1; return MyUtils.search(key2,TopTen,left,right); } public static int search(int key2,Song TopTen[],int left,int right) { int thesi=-1; int index = (left+right)/2; if (TopTen[index].position > key2) { return MyUtils.search(key2,TopTen,index++,right); } else if (TopTen[index].position < key2) { return MyUtils.search(key2,TopTen,left,index++); } else { thesi = index; } return thesi; } }