1

I'm very new to java and I know I'm learning this in a very bad way. So, I've understood a bit like how to print and stuff (I'm so sorry I'm totally new to java). Anyways, I'm creating a POS (Point of Sale) system.

So in my current code when you run this in cmd/console

You will see 2 choices (1 & 0) but it has actually 3 choices,(1,2 & 0).

1- Add Item to List

2- Total of everything (need to add items first [1])

0- Exit

After adding a product/item to the list, it would loop back to the transaction part (1,2 & 0) part. Everything was ok until I inputted "2" which then placed me with an error of:

'Exception in thread "main" java.lang.NullPointException at Pos.main(Pos.java:69)'

while my friend who I was chatting with at Facebook used this online compiler and it says this:

'java.util.Scanner.throwFor(Scanner.java:862)
    at java.util.Scanner.next(Scanner.java:1485)
    at java.util.Scanner.nextInt(Scanner.java:2117)
    at java.util.Scanner.nextInt(Scanner.java:2076)
    at Pos.main(Pos.java:47)'

(the main reason why I placed so many imports)

please help a rookie out :--(

I've been searching around stackoverflow and google but nothing seems to work or maybe my knowledge level is just too low to understand those.

anyways, my current goal is for when i press '2' it will get the total of all totals from the items at the transaction area that i've added. So basically pressing '1' at the transaction area means you're ordering something. And when I press '2' then there goes your bill (total) or something like that.

import java.*;
import java.util.*;
import java.util.Scanner;
import java.lang.*;
import java.io.*;

class Product {
      private String prodName;
      private double prodPrice;
      private int quantity;
      public double prodTotal;


  // constructor
      public Product(String prodName, double prodPrice,int quantity,double prodTotal) {
      this.prodName = prodName;
      this.prodPrice = prodPrice;
      this.quantity = quantity;
      this.prodTotal = prodTotal;
   }

       // getter
       public String getprodName() { return prodName; }
       public double getprodPrice() { return prodPrice; }
       public int getquantity() { return quantity; }
       public double getprodTotal() { return prodTotal; }

       // setter

       public void setprodName(String prodName) { this.prodName = prodName; }
       public void setprodPrice(double prodPrice) { this.prodPrice = prodPrice; }
       public void setquantity(int quantity) {this.quantity = quantity; }
       public void setprodTotal(double prodTotal) {this.prodTotal = prodTotal; }
    }
public class Pos{


        public static void main (String args[])
        {

        String pname;
        double price;
        int quant,choice,j,i=0;
        double ptotal, total=0;

        Product[] prod = new Product[100];

        do{
            System.out.println("\n1 - Add Item to List\n0 - Exit\n");
            Scanner inputReader= new Scanner( System.in );
            choice=inputReader.nextInt();
            switch(choice){
                case 1:
                    System.out.println("Enter product name: ");
                    pname = inputReader.next();
                    System.out.println("Enter price: ");
                    price = inputReader.nextDouble();
                    System.out.println("Enter quantity: ");
                    quant = inputReader.nextInt();
                    ptotal = price*quant;
                    ptotal = Math.round(ptotal * 100.0) / 100.0;
                    System.out.println("\nProduct: "+pname+"\tPrice: P"+price+"\tQuantity: "+quant+"\tTotal: P"+ptotal);
                    prod[i] = new Product (pname,price,quant,ptotal);
                    i++;
                        break;
                case 2:
                    j=i;
                    while(j>0){
                        total=total+prod[j].prodTotal;
                        j--;
                    }
                    System.out.println("Overall Total: "+total);
                        break;
                case 0:
                    break;
                    }
        }while(choice!=0);
        //System.out.println("\nProduct: "+prodName+"\tPrice: "+prodPrice+"\tQuantity: "+quantity);
        }


    }
Jonas
  • 121,568
  • 97
  • 310
  • 388
Nolan Kr
  • 81
  • 12
  • tbh this is my friend's code so I'm kinda confused too ... sorry in advanced to whatever will be commented towards how he coded it. I'm just helping him but I too am not too knowledgeable in Java xD – Nolan Kr Nov 15 '16 at 16:03
  • 1
    Your last `i++` when adding to the list make `j=i` start after the last item in the `while` loop for case 2. – bradimus Nov 15 '16 at 16:03
  • In your Pos class, which of these lines are line 69? – mdewit Nov 15 '16 at 16:03
  • line 69 would be total=total+prod[j].prodTotal; @mdewit – Nolan Kr Nov 15 '16 at 16:07
  • Edit: bradimus is right. You will be accessing an uninitialised product on this line: total=total+prod[j].prodTotal. This due to j having been set to i, after i has been incremented. So basically you are trying to access the element after the one that you allocated with prod[i] = new Product (pname,price,quant,ptotal); – mdewit Nov 15 '16 at 16:09
  • oh my god i didn't see his comment. i'm so sorry. it's pretty late here and my eyes are burning. let me change it real quick – Nolan Kr Nov 15 '16 at 16:10
  • The problem is that you are initialize j with two (value of i) in your second case. After it you want to get the second element of your array (total=total+prod[j].prodTotal;) --> Nullpointer because an array starts counting at zero and not one! Just change your line to: total=total+prod[j-1].prodTotal; and it should work for your described case. – endkugelfang Nov 15 '16 at 16:14
  • OMG @endkugelfang thank you so much. And the other guys too mdewit and bradimus. I get it now xD oh dear... I'm really sorry for not getting it directly in the first place. I'm still a student with low knowledge of these things but I'm trying to work my way to the top hehe. Thanks guys! – Nolan Kr Nov 15 '16 at 16:23
  • so the only problem I have now is math. Since #2 adds all the total, it would then add again all totals when I input another product. e.g. I press '1' and input. Total of 1 is 500. I press '2' and see all total as 500. I press '1' again and Total of this '1' is 600. I press '2' then all total becomes 500+500(another 500 huhu because it's running the equation again i think) + 600 which makes it 1600 when the answer is supposed to be 500+600 = 1100 ------ i'll try to solve it. thanks again guys!! :D <3 – Nolan Kr Nov 15 '16 at 16:28

0 Answers0