-1

Hi guys I just got started with programming and I'm completely fresh to Java. Basically I'm getting a java.lang.NullPointerException error using BlueJ using the startScan() method once I input a value in the console window:

import java.util.*;

public class Machine
{
    private double balance;
    private int count;
    private ItemTable Database;
    ArrayList<ItemType> items = new ArrayList<>();
    ArrayList<ItemType> receipt = new ArrayList<>();

    public Machine()
    {
        balance = 0;
        count = 0;
    }

    public void console()
    {
        startScan();
    }

    public void startScan()
    {
        System.out.println("Enter 'y' to begin, or 's' to shutdown.");

        Scanner input = new Scanner(System.in);
        boolean shutdown = false;
        while (!shutdown)
        {
            String line = input.nextLine();

            if (line.equals("s")){
                shutdown = true;
                System.exit(0);
            }

            else if (line.equals("y")){
                System.out.println("Enter barcode or press 's' to checkout.");
                scanItem();
            }

    }
}

    public void scanItem()
    {
        Scanner input = new Scanner(System.in);
        String line = input.nextLine();
        if(Database.barcodeValidate(line))
        {
            balance += Database.getPrice(line);
            count += 1;
            receipt.add(new ItemType ("line", Database.getName(line), Database.getPrice(line)));
            scanItem();
        }

        else if(line.equals("s"))
        {
            finishScan();
        }

        else
        {
            System.out.println("Invalid barcode.");
            scanItem();
        }

    }


    public void finishScan()
    {
    for(ItemType i : receipt) {
            System.out.println(i.getName() + ", $"+i.getPrice());

    }
    System.out.println("For "+count+" items scanned, Total price: $"+balance +".");
    }
}

From another class:

import java.util.ArrayList;

public class ItemTable
{ 
    ArrayList<ItemType> items = new ArrayList<>();

public ItemTable()
    {
        items.add(new ItemType("0", "Orange", 3.50));
        items.add(new ItemType("1", "Apple", 2));
        items.add(new ItemType("2", "Lemon", 1.50));
        items.add(new ItemType("3", "Garbage", 7.50));
        items.add(new ItemType("4", "Grass", 20.00));
        items.add(new ItemType("5", "Coke", 200));
        items.add(new ItemType("6", "Lexus ISF", 120000));
        items.add(new ItemType("7", "Lexus is250", 65000));
        items.add(new ItemType("8", "Lexus is350", 72000));
        items.add(new ItemType("9", "Lexus RC350", 78000));

    }    

    public String getName(String Barcode)
    {
        for(ItemType i : items) {
            if(Barcode.equals(i.getBarcode())) {
                return i.getName();
            }
            }
            return("Invalid barcode");
        }

    public double getPrice(String Barcode)
    {
        for(ItemType i : items) {
            if(Barcode.equals(i.getBarcode())) {
                return i.getPrice();
            }
        }
        return (-1);
    }

    public boolean barcodeValidate(String Barcode)
    {
        if(Barcode.equals("0") || Barcode.equals("1") || Barcode.equals("2") || Barcode.equals("3") || Barcode.equals("4") || Barcode.equals("5") || Barcode.equals("6") || Barcode.equals("7") || Barcode.equals("8") || Barcode.equals("9")) {
            return true;
        }
        else {
            return false;
        }
    }

}

I'm trying to make it so that if the barcode is valid (A string 0-9), the item will be scanned into the machine but keep getting the nullpointer error.

Elet
  • 11
  • 2
  • 1
    Take a look at this: [mcve] – RaminS Jun 01 '16 at 12:50
  • Is this "other" class called `ItemTable`? – Keiwan Jun 01 '16 at 12:53
  • Did you create the **Database** object? You have to create an object to use *non static* methods. For the next time, suggest you to paste also the output of the error, it is difficult to understand the nature of the error without an output. – Niles Jun 01 '16 at 12:56
  • 1
    `Database` is only declared, not instantiated. As constructed, it's `null` and can't be used. – Drew Kennedy Jun 01 '16 at 12:59
  • Did you initialize `receipt`? You are writing to the list but you are not initializing the list. Same for `Database` Sidenote: use proper naming conventions for your variable names. They should start with a lowercase letter and proceed with a caps for every noun. – Bumbolt Jun 01 '16 at 13:02
  • 1
    When asking about an exception you can't figure out, please include a stack trace, which will help us see what line the exception is referring to. Also, the stack trace will have line numbers, but we can't tell from people's questions what the line numbers are (since the questions usually have just parts of files), so you'll need to help indicate what lines the stack trace is talking about. – ajb Jun 01 '16 at 13:02

1 Answers1

0

Your variable "Database" is nowhere initialized (at least not in the code that you are showing to us). You need to have somewhere a line

Database = new ItemTable();

Beside that:

  • in Java your variable names should start with a small letter.
  • if you compare strings to a constant it's better to write the constant first (like: "0".equals(barcode) ). This has the advantage that you are not getting a NullPointerException in case barcode is null.
Joachim Rohde
  • 5,915
  • 2
  • 29
  • 46