user1311286
user1311286

Reputation:

Java, cannot access method within same class

My problem comes when buildInven(bigPath.getText()) is called, I keep getting a

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at TestLayout06$InputPanel.actionPerformed(TestLayout06.java:112)

thrown. Any ideas what my problem could be?

CODE:

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.StringTokenizer;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestLayout06 {

public static void main(String[] args) 
{
    new TestLayout06();
}

public TestLayout06() 
{
    EventQueue.invokeLater(new Runnable() 
    {
        @Override
        public void run() 
        {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (ClassNotFoundException ex) {
            } catch (InstantiationException ex) {
            } catch (IllegalAccessException ex) {
            } catch (UnsupportedLookAndFeelException ex) {
            }

            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLayout(new BorderLayout());
            frame.add(BorderLayout.NORTH, new InputPanel());
            frame.add(BorderLayout.CENTER, new InfoPanel());
            frame.add(BorderLayout.SOUTH, new CLInfoPanel());
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setSize(500, 500);
            frame.setVisible(true);

        }            
    });
}

protected class InputPanel extends JPanel implements ActionListener {

    int InventorySize = 2000;
    String AItem[] = new String[InventorySize];
    String Active[] = new String[InventorySize];
    String IdNum[] = new String[InventorySize];

    String Item[] = new String[InventorySize];
    String BNumber[] = new String[InventorySize];
    String Descrip[] = new String[InventorySize];
    String Retail[]  = new String[InventorySize];
    String Disc[] = new String[InventorySize];
    String Price[]  = new String[InventorySize];

    JTextArea bigPath = null;
    JButton bigOk = null;
    JTextArea lilPath = null;
    JButton lilOk = null;

    public InputPanel() 
    {            
        JLabel label = new JLabel("Menu");
        setLayout(new GridBagLayout());

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        add(label, gbc);

        JTextArea bigPath = new JTextArea("C:\\Users\\User\\Documents\\InputInventory.txt", 1, 30);
        JButton bigOk = new JButton("Load Inventory");
        JTextArea lilPath = new JTextArea("C:\\Users\\User\\Documents\\ItemsMarked.txt", 1, 30);
        JButton lilOk = new JButton("Load Info");

        bigOk.addActionListener(this);
        lilOk.addActionListener(this);


        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridy++;
        add(bigPath, gbc);
        gbc.gridy++;
        add(bigOk, gbc);
        gbc.gridy++;
        add(lilPath, gbc);
        gbc.gridy++;
        add(lilOk, gbc);
    }

    public void actionPerformed(ActionEvent evt) 
    {
        String str = evt.toString();
        boolean inv = str.contains("Load Inventory");
        boolean item = str.contains("Load Info");
        if (inv)
            this.buildInven(this.bigPath.getText());
        if (item)
            this.buildCLInf(lilPath.getText());
    }

    public void buildInven(String arg)
    {
        Scanner in = null;
        try
        {
            in = new Scanner(new File(arg));
        }
        catch (FileNotFoundException e)
        {
            bigPath.setText(arg + " not found");
        }
        int i = 0;
        while (in.hasNextLine())
        {
            int j = 0;
            StringTokenizer tkner = new StringTokenizer(in.nextLine());
            while (tkner.hasMoreTokens())
            {
                if(j == 0)
                    Item[i] = tkner.nextToken("|");
                else if(j == 1)
                    BNumber[i] = tkner.nextToken("|");
                else if(j == 2)
                    Descrip[i] = tkner.nextToken("|");
                else if(j == 3)
                    Retail[i] = tkner.nextToken("|");
                else if(j == 3)
                    Disc[i] = tkner.nextToken("|");
                else if(j == 5)
                    Price[i] = tkner.nextToken("|");
                else
                    tkner.nextToken("|");
                 j++;
            }
            i++;
        }
        in.close();
    }

    public void buildCLInf(String arg)
    {
        Scanner in = null;
        try
        {
            in = new Scanner(new File(arg));
        }
        catch (FileNotFoundException e)
        {
            lilPath.setText(arg + " not found");
        }
        int i = 0;
        while (in.hasNextLine())
        {
            int j = 0;
            StringTokenizer tkner = new StringTokenizer(in.nextLine());
            while (tkner.hasMoreTokens())
            {
                if(j == 0)
                    AItem[i] = tkner.nextToken("|");
                else if(j == 1)
                    Active[i] = tkner.nextToken("|");
                else if(j == 2)
                    IdNum[i] = tkner.nextToken("|");
                else
                    tkner.nextToken("|");
                 j++;
            }
            i++;
        }
        in.close();
    }

}

protected class InfoPanel extends JPanel {

    public InfoPanel() {            
        JLabel label = new JLabel("Menu");
        setLayout(new GridBagLayout());

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        add(label, gbc);

        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridy++;
        add(new JButton("Option 1"), gbc);
        gbc.gridy++;
        add(new JButton("Option 2"), gbc);
        gbc.gridy++;
        add(new JButton("Option 3"), gbc);
    }
}

protected class CLInfoPanel extends JPanel {

    public CLInfoPanel() {            
        JLabel label = new JLabel("Menu");
        setLayout(new GridBagLayout());

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        add(label, gbc);

        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridy++;
        add(new JButton("Option 1"), gbc);
        gbc.gridy++;
        add(new JButton("Option 2"), gbc);
        gbc.gridy++;
        add(new JButton("Option 3"), gbc);
    }
}
}

Upvotes: 0

Views: 167

Answers (2)

Brian Roach
Brian Roach

Reputation: 76888

You're getting an NPE becaue you've declared bigPath a second time inside the method therefore you're shadowing/hiding the one you think you're using (the one declared as a field in the class).

Change:

JTextArea bigPath = new JTextArea("C:\\Users\\User\\Documents\\InputInventory.txt", 1, 30);

to:

bigPath = new JTextArea("C:\\Users\\User\\Documents\\InputInventory.txt", 1, 30);

This applies to all the other variables you're redeclared in that scope as well.

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1499760

The problem is that your bigPath field is still null. In your constructor, you're declaring a local variable and initializing that:

JTextArea bigPath = new JTextArea(...);

That doesn't change the value of your field at all. Therefore the value of the field remains at its default of null, so when you execute this code:

bigPath.getText()

... you get the NullPointerException.

Just change the above to:

bigPath = new JTextArea(/* code as before */);

... and do the same for bigOk, lilPath and lilOk too, where you've got the same mistakes.

That's not necessarily all that's wrong in your code, but that's why you're getting the exception that you're getting.

Upvotes: 3

Related Questions