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 17 of 17

Thread: NullPointerException - don't know why

  1. #1
    Junior Member
    Join Date
    Aug 2019
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default NullPointerException - don't know why

    hello,
    I am writing a program of a library.
    I have a class of magazine that contains an arrayList of papers and looks like that:
    public class Magazine extends LibraryCollection {
     
        private ArrayList<Paper> papersCollection; //a collection of all the papers
     
     
        //constructor to initialize all of the fields
        public Magazine(String name, int number, ArrayList<Paper> papers) {
            super(name, number); //calling the superclass constructor to initialize his fields
            papersCollection = new ArrayList<Paper>(papers);
        }

    I also have this method in the class:
    //method to get the array list    public ArrayList<Paper> getPapersCollection() {
            return papersCollection;
        }

    I have a library class with this method:
    //method to return all of the magazine papers
        public String getMagazinePapers(Magazine magazine)
        {
            ArrayList<Paper> papers = new ArrayList<Paper>();
            papers = magazine.getPapersCollection();
            if(magazines.contains(magazine))
            {
                String magazinePapers = new String();
                magazinePapers = papers.toString();
                return magazinePapers;
            }
            else
            {
                return null;
            }
        }

    And this are the releveant lines from main:
    private static Library LibrarySys;
    .
    .
    .
    if (command.equals("addMagazine")) {
                    String magazineName = input.next();
                    ArrayList<Paper> listOfPapers = new ArrayList<Paper>();
                    // build papers list
                    listOfPapers.add(new Paper("paper1", Topic.CS, new Author("author5Name", "author5LastName", Topic.CS), PaperValue.CONFERENCE,"Haifa university" ));
     
                    listOfPapers.add(new Paper("paper2", Topic.MATH,new Author("author6Name", "author6LastName", Topic.MATH), PaperValue.MAGAZINE,"MIT university" ));
                    listOfPapers.add(new Paper("paper3", Topic.CHEMISTRY,new Author("author7Name", "author7LastName", Topic.CHEMISTRY), PaperValue.WORK_SHOP,"Oxford university" ));
                    listOfPapers.add(new Paper("paper4", Topic.SOCIOLOGY,new Author("author8Name", "author8LastName", Topic.SOCIOLOGY), PaperValue.CONFERENCE,"Tel Aviv university" ));
                    listOfPapers.add(new Paper("paper5", Topic.CS,new Author("author5Name", "author5LastName", Topic.CS), PaperValue.MAGAZINE,"haifa university" ));
                    listOfPapers.add(new Paper("paper6", Topic.PHYSICS,new Author("author9Name", "author9LastName",  Topic.PHYSICS), PaperValue.CONFERENCE,"Yael university" ));
     
                    int id = 2;
                    //create magazine
                    Magazine magazine = new Magazine(magazineName, id, listOfPapers); 
     
     
                    // add to library
                    if (LibrarySys.addMagazine(magazine)) { // if adding successfully, then true returned,
                        // the following message is written to the output file
                        MyFileLogWriter.writeToFileInSeparateLine("Successfully adding magazine: " + magazineName );
                    } else {
                        MyFileLogWriter.writeToFileInSeparateLine("Failed to add magazine review");
                    }
     
     
     
                }
    // ================ Building Command ================================================
                if (command.equals("getMagazinePapers")) {
                    String magazineName = input.next();
                    Magazine magazine = LibrarySys.getMagazine(magazineName);
     
     
                    if (magazine == null)
                        MyFileLogWriter.writeToFileInSeparateLine("no magazine with this name");
                    else
                        MyFileLogWriter.writeToFileInSeparateLine(LibrarySys.getMagazinePapers(magazine).toString());
                }

    I have the NullPointerException in the lines: magazinePapers = papers.toString(); and: MyFileLogWriter.writeToFileInSeparateLine(LibraryS ys.getMagazinePapers(magazine).toString());
    I would love to know why I am getting this exception.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: NullPointerException - don't know why

    I have the NullPointerException in the lines: magazinePapers = papers.toString();

    why I am getting this exception
    You get that exception when trying to use a variable that has a null value. What value is in the papers variable?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Aug 2019
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: NullPointerException - don't know why

    The papers array list initalized in the main with this lines:
                    listOfPapers.add(new Paper("paper1", Topic.CS, new Author("author5Name", "author5LastName", Topic.CS), PaperValue.CONFERENCE,"Haifa university" ));
                    listOfPapers.add(new Paper("paper2", Topic.MATH,new Author("author6Name", "author6LastName", Topic.MATH), PaperValue.MAGAZINE,"MIT university" ));
                    listOfPapers.add(new Paper("paper3", Topic.CHEMISTRY,new Author("author7Name", "author7LastName", Topic.CHEMISTRY), PaperValue.WORK_SHOP,"Oxford university" ));
                    listOfPapers.add(new Paper("paper4", Topic.SOCIOLOGY,new Author("author8Name", "author8LastName", Topic.SOCIOLOGY), PaperValue.CONFERENCE,"Tel Aviv university" ));
                    listOfPapers.add(new Paper("paper5", Topic.CS,new Author("author5Name", "author5LastName", Topic.CS), PaperValue.MAGAZINE,"haifa university" ));
                    listOfPapers.add(new Paper("paper6", Topic.PHYSICS,new Author("author9Name", "author9LastName",  Topic.PHYSICS), PaperValue.CONFERENCE,"Yael university" ));

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: NullPointerException - don't know why

    That code in post#3 uses the listOfPapers variable, not the papers variable. Where is the papers variable assigned a valid value?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Aug 2019
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: NullPointerException - don't know why

    this is how the papers class looks:
    public class Paper extends LibraryItem {
     
    	private PaperValue value; //the value of the paper
    	private String universityName; //the university name of the paper
     
    	//constructor to initialize all of the fields
    	public Paper(String name, Topic topic, Author auther, PaperValue valueOfPaper,
    			String university) {
    		super(name, topic, auther); //calling the superclass constructor to initialize his fields
    		value = valueOfPaper;
    		universityName = university;
    	}

    And this is the how the libraryIten class looks:
    //this class represents a item in the library
    public class LibraryItem {
     
    	protected String itemName; //the name of the item
    	protected Topic itemTopic; //the topic of the item from the Topic class
    	protected Author authorOfItem; //the author of the item
    	protected ArrayList<Review> itemReviews; //array list that contains all of the reviews of the item
    }

    All of the papers and the magazine are initalized in the main class

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: NullPointerException - don't know why

    What is in the papers variable just before the statement where the NPE happens?
    Add a print statement that prints its value just before the line where the NPE happens:
      System.out.println("papers="+papers);
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Aug 2019
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: NullPointerException - don't know why

    It prints papers=null but I don't understand why is is empty beacuse there are papers in the magazine

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: NullPointerException - don't know why

    papers=null but I don't understand why
    Where is the variable papers assigned a valid value?
    Add that same print statement after EVERY place where papers is assigned a value to see where the null value is coming from:
      papers = ... something or method   //  assigns a value to papers
      System.out.println("1papers="+papers);  // show value assigned
    Change the "1papers" to another value so that all the println statements have unique values that allow you to find which println statement printed the null value
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Aug 2019
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: NullPointerException - don't know why

    I added prints and I faund out that the problem is with the addMagazine method.
    This is the method:
    //method to add a magazine to a library
    	public boolean addMagazine(Magazine magazine)
    	{
    		boolean exist = magazines.contains(magazine); //checks if the magazine already exist
    		if(exist) //if the magazine exist
    		{
    			return false; //doesn't add the magazine
    		}
    		else //if the magazine doesn't exist
    		{
    			magazines.add(magazine); //add it to the library
    			return true; //return true if was added successfully
    		}
    	}

    I don't understand what is the problem with it.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: NullPointerException - don't know why

    I faund out that the problem is with the addMagazine method.
    What was the problem that you found? Can you describe what you saw and why you think that is the problem?
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Aug 2019
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: NullPointerException - don't know why

    I put a printing line in the main after calling the addMagazine method so I think the problem is in the addMagazine and that it doesn't add the magazine properly. Maybe this also why the getMagazinePapers doesn't working.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: NullPointerException - don't know why

    Can you post the code where the print statement is located?
    And copy the console's contents and paste it here that shows what is printed.

    a printing line in the main after calling the addMagazine method
    Did you put a print statement after every location where the papers variable is assigned a value?
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Aug 2019
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: NullPointerException - don't know why

    Here are all the prints I have:
    if (command.equals("addMagazine")) {
    				String magazineName = input.next();
    				ArrayList<Paper> listOfPapers = new ArrayList<Paper>();
    				// build papers list
    				listOfPapers.add(new Paper("paper1", Topic.CS, new Author("author5Name", "author5LastName", Topic.CS), PaperValue.CONFERENCE,"Haifa university" ));
    				System.out.printf("papers.0 is not null: %b%n", listOfPapers.get(0));
    				listOfPapers.add(new Paper("paper2", Topic.MATH,new Author("author6Name", "author6LastName", Topic.MATH), PaperValue.MAGAZINE,"MIT university" ));
    				System.out.printf("papers.1 is not null: %b%n", listOfPapers.get(1));
    				listOfPapers.add(new Paper("paper3", Topic.CHEMISTRY,new Author("author7Name", "author7LastName", Topic.CHEMISTRY), PaperValue.WORK_SHOP,"Oxford university" ));
    				System.out.printf("papers.2 is not null: %b%n", listOfPapers.get(2));
    				listOfPapers.add(new Paper("paper4", Topic.SOCIOLOGY,new Author("author8Name", "author8LastName", Topic.SOCIOLOGY), PaperValue.CONFERENCE,"Tel Aviv university" ));
    				System.out.printf("papers.3 is not null: %b%n", listOfPapers.get(3));
    				listOfPapers.add(new Paper("paper5", Topic.CS,new Author("author5Name", "author5LastName", Topic.CS), PaperValue.MAGAZINE,"haifa university" ));
    				System.out.printf("papers.4 is not null: %b%n", listOfPapers.get(4));
    				listOfPapers.add(new Paper("paper6", Topic.PHYSICS,new Author("author9Name", "author9LastName",  Topic.PHYSICS), PaperValue.CONFERENCE,"Yael university" ));
    				System.out.printf("papers.5 is not null: %b%n", listOfPapers.get(5));
    				int id = 2;
    				//create magazine
    				Magazine magazine = new Magazine(magazineName, id, listOfPapers); 
    				System.out.printf("papers.6 is not null: %b%n", magazine);
    				// add to library
    				if (LibrarySys.addMagazine(magazine)) { // if adding successfully, then true returned,
    					// the following message is written to the output file
                                     System.out.printf("papers.7 is not null: %b%n", magazine);
    					MyFileLogWriter.writeToFileInSeparateLine("Successfully adding magazine: " + magazineName );
    				} else {
    					MyFileLogWriter.writeToFileInSeparateLine("Failed to add magazine review");
    				}
    			}
    // ================ Building Command ================================================
    			if (command.equals("getMagazinePapers")) {
    				String magazineName = input.next();
    				Magazine magazine = LibrarySys.getMagazine(magazineName);
    				System.out.printf("papers.8 is not null: %b%n", magazine);
    				if (magazine == null)
    					MyFileLogWriter.writeToFileInSeparateLine("no magazine with this name");
    				else
    					MyFileLogWriter.writeToFileInSeparateLine(LibrarySys.getMagazinePapers(magazine).toString());
    			}

    And this is the output:
    papers.0 is not null: true
    papers.1 is not null: true
    papers.2 is not null: true
    papers.3 is not null: true
    papers.4 is not null: true
    papers.5 is not null: true
    papers.6 is not null: true
    Exception in thread "main" java.lang.NullPointerException
    at Model.Test.main(Test.java:34)

    The line that has the eror is: if (LibrarySys.addMagazine(magazine)){
    Last edited by daniella; August 5th, 2019 at 08:46 AM.

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: NullPointerException - don't know why

    That is NOT what I asked you to do. Here is the code I posted:
      papers = ... something or method   //  assigns a value to papers
      System.out.println("1papers="+papers);  // show value assigned
    Your code is printing out something different, not the value of the variable: papers.

    Exception in thread "main" java.lang.NullPointerException
    at Model.Test.main(Test.java:34)
    What variable used in the statement at line 34 has a null value? Add a print statement just before line 34 that prints the values of the variables used on line 34.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Junior Member
    Join Date
    Aug 2019
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: NullPointerException - don't know why

    I added a printing line and it prints the magazine this way: magazine name: HaifaMagazine
    The problem is there is an array list of magazines in library and I don't know why it doesn't add the magazine to the array properly.

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: NullPointerException - don't know why

    I don't understand why you are printing anything but the value of the variable: papers.
    The problem was papers had a null value.
    I have the NullPointerException in the lines: magazinePapers = papers.toString();
    The NPE would be because the variable: papers had a null value.
    The question is to find out why papers has a null value. That is what the print statements are supposed to show:
    where is papers getting the null value?
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: NullPointerException - don't know why

    Also posted at: https://coderanch.com/t/714005/java/...rException-don
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. NullPointerException?
    By NTWolf1220 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 5th, 2013, 11:46 AM
  2. [SOLVED] NullPointerException
    By macko in forum What's Wrong With My Code?
    Replies: 14
    Last Post: June 21st, 2011, 11:35 AM
  3. [SOLVED] Nullpointerexception
    By kbwalker87 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: October 14th, 2010, 10:33 PM
  4. [SOLVED] NullPointerException
    By javapenguin in forum What's Wrong With My Code?
    Replies: 13
    Last Post: October 1st, 2010, 12:10 AM
  5. NullPointerException
    By bbr201 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 29th, 2010, 07:06 PM

Tags for this Thread