8 Replies - 301 Views - Last Post: 15 November 2012 - 08:52 PM Rate Topic: -----

#1 mathnerd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 29-October 12

Java is throwing a NullPointerException at my program?

Posted 14 November 2012 - 08:56 PM

* Create an array of 20 String Objects.
* Read in a list of names from the user using a sentinel value of -1 to mark the end of the list.
* Only add the name to the array if the name is not already on the list. (EX: If "Mary" is on the user list 3 times, it should only be in the Names array one time.)
* Print out the total number of names, and print out all of the names in the array.
* Remember to use methods whenever you can.


Here are my confusions and errors:

* I cannot find the exception because it seems as if I am not using an variable that was not set to an object.
* I need a way to have a count for when the names are not repeated, and a different count for when they are repeated.

import java.util.Scanner;

public class NameList
{
   public static void main(String [] args)
   {
      Scanner reader = new Scanner(System.in);
	   
      // An array of 20 String Objects is created
      String [] names = new String [20];
      
      System.out.println("Welcome to the Name List Program:-)");
      System.out.println("***********************************");
      
      // Instance variables
      String appellation;
      int count = 0;
      final String SENTINEL = "-1";
      boolean isFound;
      
      // Ask the user for a first name or "-1" if the user would not like to proceed with the program 
      System.out.print("\nEnter a first name (" + SENTINEL + " to quit): ");
      appellation = reader.nextLine();
      
      // Keeps asking the user for first names until "-1" is entered
      while (!appellation.equals(SENTINEL))
      {
         count++;
    	  
    	 System.out.print("Enter a first name (" + SENTINEL + " to quit): ");
         appellation = reader.nextLine();
      }
      
      isFound = searchForName(appellation, count, names);
      
      if (isFound == false)
      {
         System.out.println("\nThere are a total of " + count + " names.");
      
         for (int index = 0; index < count; index++)
         {
            names[index] = appellation;   
         }
   
         System.out.println("The names in the array are:");
      
         for (int index = 0; index < count; index++)
         {
     	    System.out.println(names[index]);   
         }
      }   
   } 
   
   public static boolean searchForName(String appellation, int logicalSize, String [] nameArray)
   {
      boolean answer = false;
	  
	  for (int i = 0; i < logicalSize; i++) 
      {
         if (nameArray[i].equals(appellation))
         {
            //logicalSize = logicalSize - 1;
            answer = true; 
         }
      
         else
         {
            answer = false; 
         }
      }
	  return answer;
   }
} 



Is This A Good Question/Topic? 0
  • +

Replies To: Java is throwing a NullPointerException at my program?

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2675
  • View blog
  • Posts: 11,296
  • Joined: 20-September 08

Re: Java is throwing a NullPointerException at my program?

Posted 15 November 2012 - 04:17 AM

http://technojeeves....free/118-errors
Was This Post Helpful? 0
  • +
  • -

#3 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Java is throwing a NullPointerException at my program?

Posted 15 November 2012 - 04:25 AM

I would suggest you use try...catch blocks so that you will be in a position to see where exactly in the program the exception is taking place.

regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2675
  • View blog
  • Posts: 11,296
  • Joined: 20-September 08

Re: Java is throwing a NullPointerException at my program?

Posted 15 November 2012 - 04:43 AM

Quote

so that you will be in a position to see where exactly in the program the exception is taking place.
S/he is already in that position ;)
Was This Post Helpful? 1
  • +
  • -

#5 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7621
  • View blog
  • Posts: 12,849
  • Joined: 19-March 11

Re: Java is throwing a NullPointerException at my program?

Posted 15 November 2012 - 06:23 AM

Please post a copy of the full compiler output that lists the errors.
Was This Post Helpful? 1
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Java is throwing a NullPointerException at my program?

Posted 15 November 2012 - 07:28 AM

View Postraghav.naganathan, on 15 November 2012 - 07:25 AM, said:

I would suggest you use try...catch blocks so that you will be in a position to see where exactly in the program the exception is taking place.

Regular stack trace will do the job no need to try/catch for that
Worst case, try/catch will hode were the error occurs

@OP
in the
while (!appellation.equals(SENTINEL)) loop

you read the appellation, you increment count, but you never store these appellation in the name[] array
so later on when you try to scan that array from 0 to count you encounter all the name[] slot as null
Was This Post Helpful? 1
  • +
  • -

#7 mathnerd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 29-October 12

Re: Java is throwing a NullPointerException at my program?

Posted 15 November 2012 - 08:05 PM

Exception in thread "main" java.lang.NullPointerException
at NameList.searchForName(NameList.java:66)
at NameList.main(NameList.java:40)
Was This Post Helpful? 0
  • +
  • -

#8 mathnerd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 29-October 12

Re: Java is throwing a NullPointerException at my program?

Posted 15 November 2012 - 08:05 PM

Exception in thread "main" java.lang.NullPointerException
at NameList.searchForName(NameList.java:66)
at NameList.main(NameList.java:40)
Was This Post Helpful? 0
  • +
  • -

#9 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Java is throwing a NullPointerException at my program?

Posted 15 November 2012 - 08:52 PM

View Postpbl, on 15 November 2012 - 07:58 PM, said:

View Postraghav.naganathan, on 15 November 2012 - 07:25 AM, said:

I would suggest you use try...catch blocks so that you will be in a position to see where exactly in the program the exception is taking place.

Regular stack trace will do the job no need to try/catch for that
Worst case, try/catch will hode were the error occurs

@OP
in the
while (!appellation.equals(SENTINEL)) loop

you read the appellation, you increment count, but you never store these appellation in the name[] array
so later on when you try to scan that array from 0 to count you encounter all the name[] slot as null


Thank you for that information pbl :)
mathnerd...what is happening is that your names[] is NULL and so that is the reason for your exception.

regards,
Raghav

This post has been edited by raghav.naganathan: 15 November 2012 - 08:57 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1