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.

Page 1 of 2 12 LastLast
Results 1 to 25 of 40

Thread: What's wrong?

  1. #1
    Member
    Join Date
    Oct 2013
    Posts
    58
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default What's wrong?

    Hi. My program in end stopped working. She showed me the error.

    My program:
    public class Authors2 {
     
    	int authors03;
    	String authors;
    	private Scanner scan;
    	private Scanner scan2;
    	private Scanner scan3;
    	String [] books2;
     
    	public void Authors(){
    		scan = new Scanner(System.in);
     
    		System.out.println("Please write the author: ");
    		authors = scan.nextLine();
    		}
    		//scan.close();
    		public void Authorsandbooks(){
    			scan2 = new Scanner(System.in);
     
    			System.out.println("Please write how much author books you want to write: " + authors);
    			authors03 = scan2.nextInt();
    			scan2.nextLine();
    		}
    		//scan2.close();
    		public void Books(){
    			scan3 = new Scanner(System.in);
     
    			for(int i = 0; i < authors03; i++){
     
    			System.out.println("Please write author books: " + authors);
    			books2[i] = scan3.nextLine();
    		}
    		//scan3.close();
    		}
    		public void Result(){
    			System.out.println("You have writed the author: " + authors);
    			System.out.println("You have entered these author books: " + books2);
    		}
    	public void Kiak(){
     
    	Authors();
    	Authorsandbooks();
    	Books();
    	Result();
    	}
    }

    Error's:
    [COLOR="#FF0000"]Exception in thread "main" java.lang.NullPointerException
    	at Authorandbook.Authors2.Books(Authors2.java:35)
    	at Authorandbook.Authors2.Kiak(Authors2.java:47)
    	at Authorandbook.Main.main(Main.java:10)[/COLOR]
    Please help!


  2. #2
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    563
    My Mood
    Sleepy
    Thanks
    9
    Thanked 82 Times in 76 Posts

    Default Re: What's wrong?

    please repost using code highlighting. Announcements - What's Wrong With My Code?

  3. #3
    Member
    Join Date
    Oct 2013
    Posts
    58
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: What's wrong?

    Okey.

  4. #4
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    563
    My Mood
    Sleepy
    Thanks
    9
    Thanked 82 Times in 76 Posts

    Default Re: What's wrong?

    Where did you initialize books2 ?

  5. #5
    Member
    Join Date
    Oct 2013
    Posts
    58
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: What's wrong?

    You are talking about this?:

    books2[i] = scan3.nextLine();

  6. #6
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    563
    My Mood
    Sleepy
    Thanks
    9
    Thanked 82 Times in 76 Posts

    Default Re: What's wrong?

    No, about this:
    String [] books2;

  7. #7
    Member
    Join Date
    Oct 2013
    Posts
    58
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: What's wrong?

    Hmm.. I don't know.. As I created this:

    books2[i] = scan3.nextLine();

    I was compelled to create this one.

    String [] books2;

  8. #8
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    563
    My Mood
    Sleepy
    Thanks
    9
    Thanked 82 Times in 76 Posts

    Default Re: What's wrong?

    That's a huge difference! books2 is a cupboard that has drawers. Each drawer can hold a String.
    books2[i] = scan3.nextLine();
    Says: put this String into drawer at index i. But you've never built, bought or somehow akquired that cupboard (books2).
    Reread the basic tutorial: Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)

  9. The Following User Says Thank You to PhHein For This Useful Post:

    Lukas (October 31st, 2013)

  10. #9
    Member
    Join Date
    Oct 2013
    Posts
    58
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: What's wrong?

    What can I do?...
    Program..jpg

  11. #10
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    563
    My Mood
    Sleepy
    Thanks
    9
    Thanked 82 Times in 76 Posts

    Default Re: What's wrong?

    Well you cannot assign a String to an int array (which is also null). You can only assign a String to an index of a String array, which is not null.

  12. #11
    Member
    Join Date
    Oct 2013
    Posts
    58
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: What's wrong?

    Now I changed:
    public void Books(){
    			scan3 = new Scanner(System.in);
    			for(int i = 0; i < authors03; i++){
     
    			System.out.println("Please write author books: " + authors);
    			books2 = scan3.nextLine();
    		}
    		//scan3.close();
    		}
    		public void Result(){
    			System.out.println("You have writed the author: " + authors);
    			System.out.println("You have entered these author books: ");
    			for(int i = 0; i < authors03; i++){
    			System.out.println(books2);
    			int[] books21;
    			books21 = new int[1];
    			int books2 = 0;
    			books21[0] = books2;
    		}
    		}
    But my result is:
    Test1
    Please write author books: Lukas
    Test2
    Please write author books: Lukas
    Test3
    You have writed the author: Lukas
    You have entered these author books: 
    Test3
    Test3
    Test3

  13. #12
    Member vigneshwaran's Avatar
    Join Date
    Nov 2012
    Location
    Chennai, TamilNadu
    Posts
    35
    My Mood
    Cheerful
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default Re: What's wrong?

    I found the problem in the times of scans you have been used.
    Sorry for my previous post that i had deleted, i misunderstood concept.
    As you creating scan for three times, you are prompted such a way. create scan as class level variable that holds value. and use it in place of scan2, scan3..

  14. #13
    Member
    Join Date
    Oct 2013
    Posts
    58
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: What's wrong?

    How to create? I don't know...

  15. #14
    Member
    Join Date
    Oct 2013
    Posts
    58
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: What's wrong?

    help!

  16. #15
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    3,410
    My Mood
    Mellow
    Thanks
    85
    Thanked 473 Times in 464 Posts

    Default Re: What's wrong?

    Is the description of the problem in Post #1 still accurate, or have things changed? If they've changed, please provide updated code, descriptions, errors, etc.

    As the error indicates, check line 35 for a null (uninitialized) variable and fix it. If you want more help than that, you should provide us with a short runnable example that demonstrates the problem.

  17. #16
    Member
    Join Date
    Oct 2013
    Location
    United Kingdom
    Posts
    62
    Thanks
    1
    Thanked 4 Times in 4 Posts

    Default Re: What's wrong?

    The whole issue was to assign a String into a String[]. Below is the best possible code I can come up with...
    which was removed for spoonfeeding.
    Last edited by jps; November 1st, 2013 at 07:25 AM. Reason: spoonfeeding
    Thanks and regards,
    Sambit Swain

  18. #17
    Junior Member
    Join Date
    Nov 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What's wrong?

    Use .next() instead of .nextLine();

    I forgot specifically why, but .nextLine(); is a little flawed in a way that it sometimes forgets the input or overwrites it.

  19. #18
    Member
    Join Date
    Oct 2013
    Posts
    58
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: What's wrong?

    My code:
    import java.util.Scanner;
     
    public class Authors2 {
     
    	int authors03;
    	String authors;
    	String books2;
    	private Scanner scan;
    	private Scanner scan2;
    	private Scanner scan3;
    	public void Authors(){
    		scan = new Scanner(System.in);
     
    		System.out.println("Please write the author: ");
    		authors = scan.next();
    		}
    		//scan.close();
    	public void Authorsandbooks(){
    		scan2 = new Scanner(System.in);
     
    		System.out.println("Please write how much author books you want to write: " + authors);
    		authors03 = scan2.nextInt();
    		scan2.nextLine();
    	}
    		//scan2.close();
    	public void Books(){
    		scan3 = new Scanner(System.in);
    		for(int i = 0; i < authors03; i++){
     
    		System.out.println("Please write author books: " + authors);
    		books2 = scan3.next();
    	}
    	//scan3.close();
    	}
    	public void Result(){
    		System.out.println("You have writed the author: " + authors);
    		System.out.println("You have entered these author books: ");
    		for(int i = 0; i < authors03; i++){
    		System.out.println(books2);
    	}
    }
    	public void Kiak(){
    		Authors();
    		Authorsandbooks();
    		Books();
    		Result();
    		}
    	}

    And my result is:
    Please write the author: 
    Lukas
    Please write how much author books you want to write: Lukas
    2
    Please write author books: Lukas
    Test1
    Please write author books: Lukas
    Test2
    You have writed the author: Lukas
    You have entered these author books: 
    Test2
    Test2
    Why?

  20. #19
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    3,410
    My Mood
    Mellow
    Thanks
    85
    Thanked 473 Times in 464 Posts

    Default Re: What's wrong?

    Here is the code that prints "Test2" twice. Is that the reason you've asked "Why?" by the way? Why can't you say, "Why does my program print Test2 twice when I expect it to do ". . . .(some other thing)? Don't post riddles. Tell us exactly what you need help with. Back to your code:
    		System.out.println("You have entered these author books: ");
    		for(int i = 0; i < authors03; i++){
    		System.out.println(books2);
    	}
    The loop prints the String object books2 as many times as the value stored in authors03. What are you expecting it to do?

  21. #20
    Member
    Join Date
    Oct 2013
    Posts
    58
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: What's wrong?

    I want that the program print all books which I inserted. But now, my program prints the last book.

  22. #21
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    3,410
    My Mood
    Mellow
    Thanks
    85
    Thanked 473 Times in 464 Posts

    Default Re: What's wrong?

    Better. What class (with the main() method) drives or tests this class?

    Method names should begin with lowercase letters.

  23. #22
    Member
    Join Date
    Oct 2013
    Posts
    58
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: What's wrong?

    This is my main.
    public class Main {
     
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		Authors2 library = new Authors2 ();
    		library.Kiak();
     
    	}
    }

  24. #23
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    3,410
    My Mood
    Mellow
    Thanks
    85
    Thanked 473 Times in 464 Posts

    Default Re: What's wrong?

    Your design needs to be reworked. The class Author - get rid of the 's' - should contain only info about authors like 'name', 'datesActive', 'awards', 'booksWritten', etc. The 'booksWritten' field should be a collection of Book objects, and the Book class should contain only info about books. Each author is then a new instance of Author, and the fields for each author object are populated with that author's information.

    The Author class should not have a Scanner object or methods that ask the user to input information. That should be accomplished somewhere else, in another class that builds the collection of Author and Book objects into a Library. Trying to do everything in one class as you are currently is a tangled mess, hard to keep track of, and I can see why you're confused.

  25. #24
    Member
    Join Date
    Oct 2013
    Posts
    58
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: What's wrong?

    Can you help me to rework it? Because I don't understand now how to do that..

  26. #25
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    3,410
    My Mood
    Mellow
    Thanks
    85
    Thanked 473 Times in 464 Posts

    Default Re: What's wrong?

    You need to quit declaring ignorance and get to work. You know how to create classes. Create 2 new ones, Author and Book, create the necessary fields, setters, and getters within them, and then move code you've already written into them (if any), as appropriate. A third class, LibraryManager (?), will be used to gather user input, create authors and books, etc. That third class will have the Scanner object and the methods you've written to do those parts.

Page 1 of 2 12 LastLast

Similar Threads

  1. what wrong with this??
    By sephskie in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 14th, 2012, 06:50 PM
  2. Not sure what is wrong
    By dremn2004 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: June 18th, 2011, 10:49 AM
  3. Not sure what is wrong with this
    By jwb4291 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: June 29th, 2010, 02:23 PM
  4. Something is wrong? Please help.
    By DestinyChick1225 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 29th, 2010, 07:47 AM
  5. What's wrong?!
    By deeerek in forum What's Wrong With My Code?
    Replies: 6
    Last Post: February 22nd, 2010, 06:11 PM