-4

I am trying to access java swing components from a different class. For example, I need to change the text on a button once a specific operation has been completed. I am trying to use "getters" and "setters" to do the operation(at the bottom of the section of code.) Right now I only want to "set" the text.

I have another class that calls the "set" method and tries to set the text of the chosen button. This is the main class. The line of code that is "gui.setProcessItemBtn().setText("Some Text");" Throws:

Exception in thread "main" java.lang.NullPointerException

I believe this means that there isnt anything to set the text to. Am i missing something to link my setter methods to the actual gui components?

Main class

package book;
import book.UserInterface;

/**
 *
 * @author KJ4CC
 */
public class Book   {
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
       UserInterface gui = new UserInterface();
       gui.startUI();
       gui.setProcessItemBtn().setText("Some Text");
    }

}

User Interface class

    public class UserInterface extends JFrame {
        private JButton processItem;
        private JButton confirmItem;
        private JButton viewOrder;
        private JButton finishOrder;
        private JButton newOrder;
        private JButton exit;

        public void startUI(){

            UserInterface gui = new UserInterface();
            gui.bookingUI();
        }

        public static void  bookingUI(){
            //sets windows, and pane in the UI 
            JFrame frame = new JFrame("Ye old Book stoppe");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JPanel toppane = new JPanel(new GridBagLayout());
            JPanel bottomPane = new JPanel(new GridBagLayout());
            GridBagConstraints c = new GridBagConstraints();
            frame.setSize(800, 300);
            frame.setVisible(true);
            //adds labels  to the window
        //----------------------------------------------------------BUTTOM PANE-------------------------
        //setting up buttons to be placed onto the bottompanel 
        JButton processItem = new JButton("Process Item");
        JButton confirmItem = new JButton("Confirm Item");
        JButton viewOrder = new JButton("View Order");
        JButton finishOrder = new JButton("Finish Order ");
        JButton newOrder = new JButton("New Order");
        JButton exit = new JButton("Exit");
        //adding the buttons to the pane.---------------------------------------------------------------
        GridBagConstraints b = new GridBagConstraints();
        b.insets = new Insets(5,5,5,5);
        b.ipadx = 10;
        b.ipady = 10;
        b.gridx = 1;
        b.gridy = 0;
        bottomPane.add(processItem, b);
        b.gridx = 2;
        b.gridy = 0;
        bottomPane.add(confirmItem,b);
        confirmItem.setEnabled(false);
        b.gridx = 3;
        b.gridy = 0;
        bottomPane.add(viewOrder, b);
        viewOrder.setEnabled(false);
        b.gridx = 4;
        b.gridy = 0;
        bottomPane.add(finishOrder,b);
        finishOrder.setEnabled(false);
        b.gridx = 5;
        b.gridy = 0;
        bottomPane.add(newOrder,b);
        b.gridx = 6;
        b.gridy = 0;
        bottomPane.add(exit, b);
        bottomPane.setBackground(Color.BLUE);
        frame.add(bottomPane,BorderLayout.SOUTH);
        frame.setSize(810, 310);


        }


    //Creating getters and setters to change the text for the buttons and labels. 
    public JButton setProcessItemBtn(){
         return processItem;
     }
    public JButton setConfirmItemBtn(){
         return confirmItem;
     } 
    public JButton setViewOrderbtn(){
        return viewOrder;
     }
    public JButton setFinishOrderBtn(){
         return finishOrder;
     }
    public JButton setNewOrderBtn(){
         return newOrder;
     }
    public JButton setsetExitBtn(){
         return exit;
     }
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • The value of `private JButton processItem;` was never assigned... Hint: `JButton processItem = new JButton("Process Item");` probably does not do what you think it does – OneCricketeer Aug 29 '16 at 21:53
  • Yeah i think that is what is causing the exception. JButton processItem = new JButton("Process Item"); creates an instance of JButton correct? then the text in the quotation sets the text. Is there a way to assign the buttons to the private JButton processItem? – Johnathan Yaesu Aug 29 '16 at 21:56

2 Answers2

4

Firstly, this is actually a getter, since it return's a value.

public JButton setProcessItemBtn(){
     return processItem;
 }

This would be a setter

public void setProcessItemBtn(JButton btn){
     processItem = btn;
 }

But, you don't seem to have one of those.

If you did have that method, then you could do something like this

public static void main(String[] args) {
   UserInterface gui = new UserInterface();
   gui.setProcessItemBtn(new JButton("Some Text"));
   gui.startUI();     
}

Now, that isn't perfect, nor is it guaranteed to show a button at all, but the idea of "setting" a value is what is important.


Alternatively, you may want to not use a static method or making a new JFrame when you class already extends one... Try something like this

public class UserInterface extends JFrame {

    // Button variables

    public UserInterface(){
        bookingUI();
    }

    public void bookingUI(){
        //sets windows, and pane in the UI 
        setTitle("Ye old Book stoppe");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel toppane = new JPanel(new GridBagLayout());
        JPanel bottomPane = new JPanel(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        setSize(800, 300);
        setVisible(true);

        //setting up buttons to be placed onto the bottompanel 
        processItem = new JButton("Process Item");
        confirmItem = new JButton("Confirm Item");
        viewOrder = new JButton("View Order");
        finishOrder = new JButton("Finish Order ");
        newOrder = new JButton("New Order");
        exit = new JButton("Exit");

        // etc...
    }

Then you can try UserInterface gui = new UserInterface(); and get the buttons, and set the text on them

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
3

There are multiple errors in your code:

  1. The NPE occurs due to this:

You have 2 variables called processItem, one global variable:

private JButton processItem;

and one local variable:

JButton processItem = new JButton("Process Item");

the second processItem variable only exists within the bookingUI() method and the one you're returning on your getter is the global one, which is not initialized!

If you want to initialize the global variable processItem remove this word JButton from here:

JButton processItem = new JButton("Process Item");

That's why you're returning a not initialized variable

  1. You're creating a JFrame object and extending JFrame for no reason, remove the extends JFrame part on your class.

  2. Why are your setter methods returning a value? As a convention they should be void (and set a value) and the getters should be the ones returning something.

  3. You're not indenting your code correctly

  4. You're making your JFrame visible before you have painted everything in it, it will cause you troubles later

  5. You're not placing your program inside the EDT

Community
  • 1
  • 1
Frakcool
  • 10,915
  • 9
  • 50
  • 89