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

Thread: HELP, I AM HAVING A PROBLEM WITH ADDING VALUES OF AN ARRAY TO A STRING

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

    Default HELP, I AM HAVING A PROBLEM WITH ADDING VALUES OF AN ARRAY TO A STRING

    Hi, I seem to be having a consistent error when I try to add the values of an array I created to a String. I keep getting a NullPointerException Error. I went through and checked to make sure that all the values in the array that I am trying to print out are not null. The two arrays are salespersonTotal and productTotal. I don't know what I am doing wrong. Here is my class and tester. Please help if you can. I am pretty new to JAVA and could use all the help I can get.

    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;
      }
      System.out.println(salespersonTotal[0]);
      System.out.println(salespersonTotal[1]);
      System.out.println(salespersonTotal[2]);
     }
     
     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<productTotal.length; i++)
          toString+= "\t"+productTotal[i];
      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());
     }
    }

    This is my output and error:

    60.0
    100.0
    325.0
    java.lang.NullPointerException
    at Sales.toString(Sales.java:62)
    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)
    >

    The three numbers are the values in the array. Can you all take a look.


  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: HELP, I AM HAVING A PROBLEM WITH ADDING VALUES OF AN ARRAY TO A STRING

    java.lang.NullPointerException
    at Sales.toString(Sales.java:62)
    What variable at line 62 is null? Then backtrack in your code to findout why it does NOT have a valid value.
    If you can not tell by looking add a println statement just before line 62 to print out the value of ALL the variables used in line 62.

  3. #3
    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: HELP, I AM HAVING A PROBLEM WITH ADDING VALUES OF AN ARRAY TO A STRING

    Is this a duplicate posting of the same problem?
    http://www.javaprogrammingforums.com...html#post50078

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: HELP, I AM HAVING A PROBLEM WITH ADDING VALUES OF AN ARRAY TO A STRING

    Do not post duplicate questions to the forums. I am locking this thread.

Similar Threads

  1. Problem with array values
    By Harry Blargle in forum Collections and Generics
    Replies: 5
    Last Post: September 17th, 2011, 04:05 PM
  2. Adding to Array from JavaSpace problem
    By rtumatt in forum Collections and Generics
    Replies: 2
    Last Post: September 15th, 2011, 07:11 AM
  3. Replies: 1
    Last Post: April 7th, 2010, 03:44 PM
  4. Adding to Array from JavaSpace problem
    By rtumatt in forum Collections and Generics
    Replies: 3
    Last Post: January 5th, 2010, 07:19 PM
  5. array/string problem
    By RSYR in forum Collections and Generics
    Replies: 1
    Last Post: December 18th, 2009, 10:24 PM

Tags for this Thread