Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Exception in thread "main" java.lang.NullPointerException

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Exception in thread "main" java.lang.NullPointerException

    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;
    	}
    }


  2. #2
    Junior Member
    Join Date
    Apr 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException

    btw i found an error...
    This >>> Song TopTen[] = new Song[10];
    is suposed to be like this >>> Song TopTen[] = new Song[0];

  3. #3
    Junior Member
    Join Date
    Apr 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException

    Anyone guys????
    It's urgent plz.....

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,447
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 544 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException

    I recommend you read the link in my tutorial on asking smart questions. First off, you've posted way too much code. We have hundreds of posts here, so we simply do not have time to wade through entire programs. Instead, boil your problem down to an SSCCE and ask a specific question. More than, say, 25 lines or so is probably too much code.

    Also, mentioning your urgency will actually decrease your chances of getting help. Like I said, we have hundreds of posts here, each with an urgent user waiting on a response. Mentioning your urgency makes you seem impatient, or like you value your time more than the time of other users. And many of our users do not speak English as a native language, so using text-message style abbreviations like "plz" will not only confuse them, but it will make you seem lazy to those of us who are native English speakers.

    Anyway, like I said, throw together that SSCCE and we'll go from there.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Exception in thread "main" java.lang.NullPointerException
    By MryJaho in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 4th, 2011, 04:36 PM
  2. Replies: 6
    Last Post: November 12th, 2010, 03:40 AM
  3. Replies: 16
    Last Post: August 27th, 2010, 03:30 PM
  4. Replies: 2
    Last Post: March 26th, 2010, 11:22 AM
  5. Please help! Exception in thread "main" java.lang.NullPointerException
    By Arutha2321 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 18th, 2009, 01:25 AM