Results 1 to 4 of 4
  1. #1
    newProgrammer is offline Member
    Join Date
    Mar 2012
    Posts
    8
    Rep Power
    0

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

    Hi im new to programing and doing forums(this is first forum ive made srry for it not being formated correctly) im currently making a reusable tally class and cant figure out this problem it compiles with no problem but when i run it

    Exception in thread "main" java.lang.NullPointerException-program.png

    ive checked online and cant find the awnser as to why its doing this any help would be very appreciated
    Here is the code for the Tally program:

    import java.util.*;
    import java.io.*;

    public class Tally
    {
    private String title;
    private ArrayList<Object> Items;
    private ArrayList<Integer> Count;
    private int count = 0;


    public Tally()
    {
    title = ("unnamed tally");
    Items = new ArrayList<Object>();
    Count = new ArrayList<Integer>();
    }
    public Tally(String name)
    {
    String title = (name);
    Items = new ArrayList<Object>();
    Count = new ArrayList<Integer>();
    }

    public void add(Object item)
    {
    add(1, item);
    }

    public void add(int i, Object item)
    {
    int index = 0;
    String counter;
    int curCount;

    index = Items.indexOf(item);
    if(index < 0)
    {
    Count.add(i);
    Items.add(item);
    ++count;
    }
    else
    {
    Items.add(item); // add new object with count of 1
    Count.add(i); // updated for v5.0, includes autoboxing
    ++count;
    }
    }

    public void listAll(PrintStream out)
    {

    for(int i=0; i<Items.size(); i++)
    out.println(Count.get(i)+"\t"+Items.get(i));
    }

    public void remove(String item)
    {
    remove(1, item);
    }

    public void remove(int delCount, Object item)
    {
    int index = 0;
    String counter;
    int curCount;
    index = Items.indexOf(item);

    if(index > 0)
    {
    curCount = Count.get(index);
    curCount -= delCount;
    if(curCount>0)
    {
    Count.set(index, curCount);
    }
    else
    {
    purge(index);
    }
    }

    }

    public void removeAll(Object item)
    {
    int index = 0;

    index = Items.indexOf(item);
    Items.remove(index);
    Count.remove(index);
    }


    public void removeAll()
    {
    Count.clear();
    Items.clear();
    }

    public int getCount()
    {
    return count;
    }

    public String getTitle()
    {
    return new String(title);
    }

    public int countOf(Object item)
    {

    int index = 0;
    String counter;
    int curCount = 0;

    index = Items.indexOf(item);

    if(index >= 0) // in list
    {
    curCount = Count.get(index); // updated for v5.0
    }

    return curCount;
    }

    public void purge(int index)
    {
    //int q = Items.indexOf(item);
    Items.remove(index);
    Count.remove(index);
    --count;
    }

    }

    And here is the code for the TallyTest(This is what we are sposed to use):


    /**
    * Chapter 09 Solution to programming assignment 7, driver program
    */
    public class TallyTest
    {
    public static void main(String[] argv)
    {
    tallyChoir();
    tallyCoins();
    }

    private static void tallyChoir()
    {
    Tally t1 = new Tally("Choir voices");

    System.out.println("Creating a new Tally of choir voices.");

    t1.add("Soprano"); // only 1 soprano!
    t1.add(10,"Alto");
    t1.add(12,"Tenor");
    t1.add(15,"Bass");

    System.out.println("The choir has the following tally of voices:");
    t1.listAll(System.out); // lists all to standard output PrintStream

    System.out.println("which is, "+t1.getCount()+" different voice parts.\n");

    System.out.println("Here is the tally of "+t1.getTitle());
    System.out.println(t1.countOf("Soprano")+" sopranos,");
    System.out.println(t1.countOf("Alto")+" altos,");
    System.out.println(t1.countOf("Tenor")+" tenors, and");
    System.out.println(t1.countOf("Bass")+" basses,");

    System.out.println("\nFirst, one Bass left.");
    t1.remove("Bass");

    System.out.println("And then 3 Altos left.");
    t1.remove(3, "Alto");

    System.out.println("Which left the choir with:");
    t1.listAll(System.out);

    System.out.println("\nThen all of the Sopranos left.");
    t1.removeAll("Soprano");
    System.out.println("Which left the choir with:");
    t1.listAll(System.out);

    System.out.println("\nThen all "+t1.countOf("Tenor")+" of the Tenors left.");
    t1.remove(t1.countOf("Tenor"), "Tenor");
    System.out.println("Which left the choir with:");
    t1.listAll(System.out);

    System.out.println("\nThen everyone left.");
    t1.removeAll();
    System.out.println("Which left:\n");
    t1.listAll(System.out);

    }

    private static void tallyCoins()
    {
    Tally coinTally = new Tally("Tally of Coins");

    System.out.println("Creating a new Tally of coins.");

    coinTally.add(12,"Quarters");
    coinTally.add(10,"Dimes");
    coinTally.add("Nickles");
    coinTally.add("Pennies");

    System.out.println("This is the tally of coins:");
    coinTally.listAll(System.out); // lists all to standard output PrintStream

    System.out.println("which is, "+coinTally.getCount()+" different coins.\n");

    System.out.println("Here is the "+coinTally.getTitle());
    System.out.println(coinTally.countOf("Quarters")+" Quarters,");
    System.out.println(coinTally.countOf("Dimes")+" Dimes,");
    System.out.println(coinTally.countOf("Nickles")+" Nickles, and");
    System.out.println(coinTally.countOf("Pennies")+" Pennies.");

    System.out.println("\nFirst, one Penny left.");
    coinTally.remove("Pennies");

    System.out.println("And then 3 Dimes left.");
    coinTally.remove(3, "Dimes");

    System.out.println("Which left:");
    coinTally.listAll(System.out);

    System.out.println("\nThen all of the Quarters left.");
    coinTally.removeAll("Quarters");
    System.out.println("Which left:");
    coinTally.listAll(System.out);

    System.out.println("\nThen all of the Nickles left.");
    coinTally.remove(7, "Nickles");
    System.out.println("Which left:");
    coinTally.listAll(System.out);

    System.out.println("\nThen everyone left.");
    coinTally.removeAll();
    System.out.println("Which left:\n");
    coinTally.listAll(System.out);
    }
    }

    if anyone will tell me how i will repost the code in the correct way

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

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

    Please use [code] tags [/code] when posting code as that is unreadable otherwise.
    You should also highlight the line that exception occurred on.

    Anyway here:
    Java Code:
    return new String(title);
    title is null.
    But I would ask why you don't simply return title?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,515
    Blog Entries
    7
    Rep Power
    20

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

    Quote Originally Posted by newProgrammer View Post
    Java Code:
    	public Tally(String name)
    	{
    		String title = (name);
    		Items = new ArrayList<Object>();
    		Count = new ArrayList<Integer>();
    	}
    In this constructor you're assigning the name parameter to a local variable (it leaves the member variable 'title' at its null value).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

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

    See?
    I couldn't spot that for the lack of code tags.
    Still, doesn't answer why they're creating a new String to return.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 5
    Last Post: 12-21-2011, 06:44 PM
  2. Replies: 5
    Last Post: 10-11-2011, 12:16 AM
  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

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •