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

Thread: NullPointerException Error

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    10
    My Mood
    Grumpy
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default NullPointerException Error

    Hi, I am new to programming. I have an assignment where I am to create a class that processes a sales slip that contains a salesperson number, product number, and the total dollar value of that product ssold for that month. I am to use a 2D Array. I created a arrays to find the product totals as well as the Salesperson total, but I get a NullPointerException Error when I run my tester. Can you see what is wrong with my code.

    Class:
    public class Sales
    {
     public Sales()
     {
     }
     
     public Sales(double[][] sales)
     {
      this.sales=sales;
     }
     
     public void salesSlip(int salespersonNumber, int productNumber, double sum)
     {
      sales [salespersonNumber-1][productNumber-1]+=sum;
     }
     public void productTotal()
     {
      int cols=0;
      double sum=0;
      double[] productTotal= new double[sales[0].length];
      for(cols=0; cols<sales[0].length; cols++)
      {
       for(int rows=0; rows<sales.length; rows++)
       {
        sum+= sales[rows][cols];
       }
       productTotal[cols]=sum;
      }
     }
     
     public void salespersonTotal()
     {
      int rows=0;
      double sum=0;
      double[] salespersonTotal= new double[sales.length];
      for(rows=0; rows<sales.length; rows++)
      {
       for(int cols=0; cols<sales[0].length; cols++)
       {
        sum+= sales[rows][cols];
       }
       salespersonTotal[rows]= sum;
      }
     
     }
     
     public void calcTotal()
     {
      double total=0;
      for(int i=0; i<5; i++)
       total+= 0;
     
     }
     
     public String toString()
     {
     
      toString="\t\tProduct 1\tProduct 2\tProduct 3\tProduct 4\tProduct 5\tTOTAL\nSalesperson 1";
      for(int i=0; i<sales[0].length; i++)
          toString+="\t"+sales[0][i]+"0";
      toString+="\t"+(salespersonTotal[0]);
      toString+="\nSalesperson 2";
      for(int i=0; i<sales[0].length; i++)
          toString+="\t"+sales[1][i];
      toString+="\t"+(salespersonTotal[1]);
      toString+="\nSalesperson 3";
      for(int i=0; i<sales[0].length; i++)
          toString+="\t"+sales[2][i];
      toString+="\t"+(salespersonTotal[2]);
      toString+="\nTOTAL\t";
      for(int i=0; i<5; i++)
          toString+= "\t0.00";
      toString+="\t"+total+"0";
     
     
      return toString;
     
     
     }
     
     private double[][] sales;
     private double[] productTotal;
     private double[] salespersonTotal;
     private String toString;
     private double total;
     
    }
     
    Tester:
     
    public class SalesTester
    {
     public static void main(String[] args)
     {
      double[][] sales={{0.00,0.00,0.00,0.00,0.00},{0.00,0.00,0.00,0.00,0.00},{0.00,0.00,0.00,0.00,0.00}} ;
      Sales tester= new Sales(sales);
     
      tester.salesSlip(1,1,20.00);
      tester.salesSlip(1,2,30.00);
      tester.salesSlip(1,3,0.00);
      tester.salesSlip(1,4,10.00);
      tester.salesSlip(1,5,0.00);
      tester.salesSlip(2,1,0.00);
      tester.salesSlip(2,2,0.00);
      tester.salesSlip(2,3,10.00);
      tester.salesSlip(2,4,0.00);
      tester.salesSlip(2,5,30.00);
      tester.salesSlip(3,1,50.00);
      tester.salesSlip(3,2,60.00);
      tester.salesSlip(3,3,50.00);
      tester.salesSlip(3,4,30.00);
      tester.salesSlip(3,5,35.00);
     
      tester.productTotal();
      tester.salespersonTotal();
      tester.calcTotal();
      System.out.println(tester.toString());
     }
    }
    Error:

    java.lang.NullPointerException
    at Sales.toString(Sales.java:61)
    at SalesTester.main(SalesTester.java:27)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at edu.rice.cs.drjava.model.compiler.JavacCompiler.ru nCommand(JavacCompiler.java:271)
    Last edited by helloworld922; November 18th, 2011 at 11:04 PM.


  2. #2
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: NullPointerException Error

    Well first thing I would do, if I were you, would to look up what a NullPointerException is.

    Quote Originally Posted by Java 7 Documentation
    Thrown when an application attempts to use null in a case where an object is required.
    Going from there I would find line 61, it was in the toString method of Sales. I would print out all the values of my variables and find which one was null. When I find the one that is null I would try to find where it was initialized. If I've done all that and I'm sure I initialized it, I would then post here showing what I have already done.

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    10
    My Mood
    Grumpy
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: NullPointerException Error

    This is what I get when I print out the values:

    System.out.println(productTotal[0]);
    System.out.println(productTotal[1]);
    System.out.println(productTotal[2]);
    System.out.println(productTotal[3]);
    System.out.println(productTotal[4]);

    Output:
    70.0
    160.0
    220.0
    260.0
    325.0
    null

    Is there a reason I am getting 6 outputs lines for only 5 lines of code? Is there something else I need to do to resolve this error? Thanks for the help.

  4. #4
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: NullPointerException Error

    What is in salespersonTotal? You are only storing the values in sale, not salespersonTotal, so what do you expect it to give other than null?

  5. #5
    Junior Member
    Join Date
    Nov 2011
    Posts
    10
    My Mood
    Grumpy
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: NullPointerException Error

    Quote Originally Posted by Mr.777 View Post
    What is in salespersonTotal? You are only storing the values in sale, not salespersonTotal, so what do you expect it to give other than null?
    I am adding the values in the salespersonTotal method.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,412
    Thanks
    49
    Thanked 2,214 Times in 2,187 Posts

    Default Re: NullPointerException Error

    Where is the println() statement that printed null?
    You show 5 println statements and 6 lines of output. What printed the last line with the null?

  7. #7
    Junior Member
    Join Date
    Nov 2011
    Posts
    10
    My Mood
    Grumpy
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: NullPointerException Error

    It was my toString, that was dumb of me.

Similar Threads

  1. [SOLVED] Error Message: Exception in thread "main" java.lang.NullPointerException etc.
    By coffecupcake in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 15th, 2011, 08:34 AM
  2. [SOLVED] Java run time error: 'java.lang.NullPointerException"
    By ChandanSharma in forum What's Wrong With My Code?
    Replies: 9
    Last Post: June 20th, 2011, 11:53 AM
  3. NullPointerException error
    By pyrotecc25 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 14th, 2011, 01:17 PM
  4. NullPointerException error
    By blazerix in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 24th, 2011, 08:59 PM
  5. Getting "AWT-EventQueue-0" java.lang.NullPointerException error
    By tryingtoJava in forum AWT / Java Swing
    Replies: 9
    Last Post: September 21st, 2009, 10:46 PM

Tags for this Thread