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

Thread: Problems with ArrayList- gives me a NullPointerException and the program crashes

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problems with ArrayList- gives me a NullPointerException and the program crashes

    public Flask[] getArtifactsByBrand(String name){
        	 Flask[] myArray = new Flask[flasks.size()];
        	 Iterator<Flask> it= flasks.iterator();
     
        	  while(it.hasNext()){
     
                      System.out.print(it.next());
     
        	 }   
     
        	 if(flasks.contains(name)) {
        		 for(int i=0; i<myArray.length; i++) {
     
               	  System.out.println(myArray[i].toString());
               	  System.out.println(flasks.toString());
     
                   }
        	 } 
                   else {
                	   System.out.println("No flasks found");
                   }
     
    		 return myArray;     
     
         }

    Well, this is my code. Now, I want to search for the brand name in the arraylist. When I search for the brand. it gives me the result and then the program crashes. Its gives me an NullPointerException.(I am guessing nothing's there) PLEASE HELP..I have been stuck on this problem for a whole

    Enter Brand: ARgos
    Flask [brand=ARgos, includedCups=9, isWatertight=true, capacity=9.0, intendedLiquid=water, hasInsulation=true, netWeight=9.0, isExhibited=false]No flasks found
     
     
    Exception in thread "main" java.lang.NullPointerException
    	at museum.MuseumApplication.searchByBrand(MuseumApplication.java:160)
    	at museum.MuseumApplication.run(MuseumApplication.java:54)
    	at museum.MuseumApplication.main(MuseumApplication.java:31)


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

    Default Re: Problems with ArrayList- gives me a NullPointerException and the program crashes

    Its gives me an NullPointerException
    Please copy the full text of the error message and paste it here. It has important info about the error.

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    May 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problems with ArrayList- gives me a NullPointerException and the program crashes

    I have posted the error above along with the code

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

    Default Re: Problems with ArrayList- gives me a NullPointerException and the program crashes

    Exception in thread "main" java.lang.NullPointerException
    at museum.MuseumApplication.searchByBrand(MuseumAppli cation.java:160)
    There is a variable with a null value on line 160. Look at line 160 in the your source and see what variable is null. Then backtrack in the code to see why that variable does not have a valid value.
    If you can not tell which variable it is, add a println just before line 160 and print out the values of all the variables on that line.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    May 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problems with ArrayList- gives me a NullPointerException and the program crashes

    Well, The code in the museum application is correct. There should be nothing wrong with that. This is actually a project.they gave us a half written application. The code in the museumApplication was all written by the teachers. We just have to complete the methods the Museum.java. When I asked my teacher baou thr "NullPointerException" error he said, its because there is nothing in myArray. But, I wasn't sure what to do.

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

    Default Re: Problems with ArrayList- gives me a NullPointerException and the program crashes

    Did you find the variable with the null value? You need to find that first. When you have found the variable with the null value, then figure out why that variable does not have a valid value.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 6
    Last Post: February 11th, 2013, 02:08 PM
  2. Problems with ArrayList
    By 93tomh in forum What's Wrong With My Code?
    Replies: 9
    Last Post: July 19th, 2012, 08:46 AM
  3. How to make an error message when my program crashes?
    By noFear in forum Java Theory & Questions
    Replies: 10
    Last Post: August 11th, 2010, 08:50 AM

Tags for this Thread