Results 1 to 12 of 12
Like Tree1Likes
  • 1 Post By Tolls

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

  1. #1
    joel7 is offline Member
    Join Date
    Nov 2012
    Posts
    22
    Rep Power
    0

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

    this is my code....i have error wen compile.
    error message : Exception in thread "main" java.lang.NullPointerException
    at Test.main(Test.java:47)


    import java.util.Scanner;

    public class Test {

    public static void main(String[] args){
    Scanner input = new Scanner(System.in);


    String bkId = null;
    String bkTitle = null;
    String bkPublisher = null;
    String bkAuthor = null;

    String mgId = null;
    String mgTitle = null;
    String mgPublisher = null;
    String mgDate1 = null;

    String npId = null;
    String npTitle = null;
    String npPublisher = null;
    String npDate2 = null;

    int booksAdd;

    System.out.println("<><><><><>Welcome to KDU UC Library Management System<><><><><>");
    System.out.println("\n1.Add Books \n2.Add Megazine \n3.Add Newspaper");
    booksAdd = input.nextInt();
    String temp0 = input.nextLine();

    switch(booksAdd){
    case 1:
    Book[] addBooks = new Book[20];

    for(int i=0; i<10; i++){
    addBooks[i] = new Book();
    }

    for(int i=0; i<10; i++){

    System.out.println("Enter ID:");
    bkId = input.nextLine();


    for(int j=0;j<addBooks.length;j++){
    while(addBooks[j].id.equals(bkId));{
    System.out.println("Duplicate ID");
    System.out.println("Enter ID for books "+(i+1) + ":");
    bkId = input.nextLine();

    }
    }

    System.out.println("Enter Title:");
    bkTitle = input.nextLine();

    System.out.println("Enter Publisher:");
    bkPublisher = input.nextLine();

    System.out.println("Enter Author:");
    bkAuthor = input.nextLine();

    addBooks[i] = new Book(bkId,bkTitle,bkPublisher,bkAuthor);


    }
    case 2:
    Megazine[] addMegazine = new Megazine[20];

    for(int i=0; i<10; i++){
    addMegazine[i] = new Megazine();
    }

    for(int i=0; i<10; i++){

    System.out.println("Enter ID:");
    mgId = input.nextLine();

    for(int j=0;j<addMegazine.length;j++){
    while(addMegazine[j].id.equals(mgId)){
    System.out.println("Duplicate ID");
    System.out.println("Enter ID for megazine "+(i+1) + ":");
    mgId = input.nextLine();
    }
    }

    System.out.println("Enter Title:");
    mgTitle = input.nextLine();

    System.out.println("Enter Publisher:");
    mgPublisher = input.nextLine();

    System.out.println("Enter Date:");
    mgDate1 = input.nextLine();

    addMegazine[i] = new Megazine(mgId,mgTitle,mgPublisher,mgDate1);
    }
    case 3:
    Newspaper[] addNews = new Newspaper[20];

    for(int i=0; i<10; i++){
    addNews[i] = new Newspaper();
    }

    for(int i=0; i<10; i++){

    System.out.println("Enter ID:");
    npId = input.nextLine();

    for(int j=0;j<addNews.length;j++){
    while (addNews[j].id.equals(npId)){
    System.out.println("Duplicate ID");
    System.out.println("Enter ID for newspaper "+(i+1) + ":");
    npId = input.nextLine();
    }
    }

    System.out.println("Enter Title:");
    npTitle = input.nextLine();

    System.out.println("Enter Publisher:");
    npPublisher = input.nextLine();

    System.out.println("Enter Date:");
    npDate2 = input.nextLine();

    addNews[i] = new Newspaper(npId,npTitle,npPublisher,npDate2);
    }
    default:
    System.out.println("Invalid");
    break;
    }
    }


    }

    ps.reply asap
    tq

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,089
    Rep Power
    20

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

    Please use [code] tags [/code] when posting code.
    Unformatted code is hard to follow.

    And when you do that you can also point out which line is causing the problem.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    joel7 is offline Member
    Join Date
    Nov 2012
    Posts
    22
    Rep Power
    0

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

    wat u mean by code tags??

    the error is afta i choose the option....den i enter id...
    afta the id i gt tat error.

  4. #4
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,089
    Rep Power
    20

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

    Java Code:
    Your code goes in here all nice and formatted
    That there is wrapped in [code] tags [/code].

    You still haven't highlighted the line it occurs on.
    The stack trace gave you a line number.

    And please don't use txt spk.
    This forum does not charge by the letter...
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,089
    Rep Power
    20

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

    Quote Originally Posted by rougeking View Post
    obviously it throws NullPointerException.......
    while using input.nextLine() JVM is expecting some input from us.....
    what if there is no input......hence it throws NullPointerException...
    No it doesn't.
    nextLine() will not throw an NPE.
    In addition, the stack trace is quite clear it is not being thrown by that method.
    Finally, nextLine() will block until data is available, so if there is no input it will sit there waiting.

    Quote Originally Posted by rougeking View Post
    we have to handle that exception......
    i prefer using try-catch block around each input.nextLine() line...
    else u can use "throws NullPointerException" beside main......

    hope its useful :)
    You should almost never catch an NPE like that, as it is almost always a sign that there is something broken in your code.
    Also there is no need to "throws NullPointerException" as it is a RuntimeException.

    Honestly, if you do not know how Java works it might be an idea for you not to answer the question.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    joel7 is offline Member
    Join Date
    Nov 2012
    Posts
    22
    Rep Power
    0

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

    can u did it for me ???
    with the try catch block

  7. #7
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,089
    Rep Power
    20

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

    Did you read what I wrote?
    rougeking is wrong.
    You do not need a try/catch.
    You need to identify what on line 47 is null and then figure out why it is null.

    We can help on that, but not if you continue to ignore what is asked of you.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    joel7 is offline Member
    Join Date
    Nov 2012
    Posts
    22
    Rep Power
    0

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

    can u told me what i should replace in line 47 ???
    can u help me

  9. #9
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,089
    Rep Power
    20

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

    I have no idea what line up there is line 47.
    I can't even follow the code as I cannot read it since it is not wrapped in code tags.

    You must know what line 47 is...which is why I keep asking you to highlight it.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  10. #10
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

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

    am i being an idiot here or has the Book[] class not been implemented into the class?
    so wouldn't it fail to compile as class test has no idea what a Book[] addBooks = new Book[20]; is?

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

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

    rougeking's advice is bad enough that I have deleted his post.

  12. #12
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,089
    Rep Power
    20

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

    Quote Originally Posted by monkeyjr97 View Post
    am i being an idiot here or has the Book[] class not been implemented into the class?
    so wouldn't it fail to compile as class test has no idea what a Book[] addBooks = new Book[20]; is?
    Don't ask me, I tend not to read large chunks of unformatted code.
    DarrylBurke likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 6
    Last Post: 04-19-2012, 07:10 PM
  2. Replies: 5
    Last Post: 12-21-2011, 06:44 PM
  3. Replies: 6
    Last Post: 07-16-2009, 03:30 PM
  4. Replies: 1
    Last Post: 07-16-2009, 10:35 AM
  5. Replies: 1
    Last Post: 07-16-2007, 06:32 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
  •