0

I am having a lot of trouble with a homework assignment. We were given three JPanels: myJPanel, myJPanel1, and myJPanel2. I have to have a JButton in mJPanel1 update the text of a button, b2, in myJPanel2. The problem is I can't make any changes to myJPanel2. Everything I try gives me a NullPointerException. Please help.

Here is the code: myJPanel-

import java.awt.*;
import javax.swing.*;
public class MyJPanel extends JPanel {
    MyJPanel panel;
    public MyJPanel() {
        super();
        setBackground(Color.gray);
        setLayout(new BorderLayout());
        MyJPanel2 p2 = new MyJPanel2();
        add(p2, "Center");
        MyJPanel1 p1 = new MyJPanel1(p2);
        add(p1, "North");
    }
}

myJPanel1-

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class MyJPanel1 extends JPanel implements ActionListener {
    JButton jl1;
    Student st1 = new Student("Fred", "Fonseca", 44);
    myJPanel panel;
    myJPanel2 panel2;
    public MyJPanel1(JPanel p2) {
        super();
        setBackground(Color.yellow);
        // the whatsUp of this student has to shown in the other panel
       jl1 = new JButton(st1.getInfo());
       jl1.addActionListener(this);
       add(jl1);
    }@
    Override
    public void actionPerformed(ActionEvent event) {
        Object obj = event.getSource();
        if (obj.equals(jl1)) {
            panel2.b2.setText("test"); //This is where the NullPointerException is...
        }
    }
}

myJPanel2-

import java.awt.*;
import javax.swing.*;
public class myJPanel2 extends JPanel {
    //==================================================
    //no changes allowed in myJPanel2 for assignment 05
    //==================================================
    JButton b1, b2, b3, b4;
    public myJPanel2() {
        super();
        setBackground(Color.pink);
        //setLayout(new GridLayout(3,1));
        b1 = new JButton("When the user clicks on the button in the UPPER panel");
        add(b1);
        b2 = new JButton("Display here whatsUp from the student in UPPER Panel");
        add(b2);
        b3 = new JButton("===>>>>You CANNOT create a student here <======");
        add(b3);
        b4 = new JButton("It has to be the student from the UPPER Panel");
        add(b4);
    }
}

Any help would be greatly appreciated. I have been stuck on this problem for days.

  • 1
    You're getting a NPE because the Panel2 variable is null, and for why, ask yourself, where do you ever assign a valid object to this field? In other words, where do you have `Panel2 = /* something */` anywhere? – Hovercraft Full Of Eels Sep 24 '16 at 15:46
  • 1
    A simple fix is to give myPanel1 a `setMyPanel2(...)` method, and then pass the valid reference to the visualized myPanel2 object into the visualized myPanel1 object. – Hovercraft Full Of Eels Sep 24 '16 at 15:47
  • 2
    Side issues: Your code formatting is not good, especially your code indentation. Understand that code formatting isn't there to make code "look good" but rather the rules are there to help you quickly see what code belongs to what scope, something that helps you debug and understand your code. You will want to put in the effort to format well, for *your* benefit, so you can more easily debug problems, and for *ours* so we can more easily understand your code and help you. This isn't a trivial request. – Hovercraft Full Of Eels Sep 24 '16 at 15:47
  • 2
    Side issue 2: you will want to learn and use [Java naming conventions](http://en.wikipedia.org/wiki/Naming_convention_(programming)#Java). Variable names should all begin with a lower letter while class names with an upper case letter. Learning this and following this will allow us to better understand your code, and would allow you to better understand the code of others. – Hovercraft Full Of Eels Sep 24 '16 at 15:48
  • As @HovercraftFullOfEels suggested "pass the valid reference" of `myJPanel2` to `myJPanel1` . Do it by using : `myJPanel1 p1 = new myJPanel1(p2);` and change `myJPanel1` constructor signature to `public myJPanel1(JPanel p2)` – c0der Sep 24 '16 at 15:59
  • 2
    Stop extending `JPanel` & most of the problems disappear. Really no case there for extending in any of the 3 cases seen above. – Andrew Thompson Sep 24 '16 at 16:00
  • @HovercraftFullOfEels Sorry about the messy code. I'm a new programmer, and I haven't slept in about three days. I need to get better at that. The naming conventions used came from the base code my teacher gave me, so I just wanted to get a working solution before I edited anything else. – Face1ess74 Sep 24 '16 at 17:46
  • @HovercraftFullOfEels I see what you mean. I didn't initialize Panel2, so when I refer to it, there is nothing to point at. What should I set Panel2 equal to, and in which JPanel should I do it in? – Face1ess74 Sep 24 '16 at 17:55
  • @Face1ess74: this is so confusing still since you're continuing to use capital letters to start variable names. The bottom line is what I told you -- the visualized object from one should be passed into the visualized object for the other. Meaning you can't just create a new object and assign it to panel2, but rather it has to be the one displayed. Much better would be to get the logic out of the GUI classes entirely, using Model-View-Controller type program design, but that may be too advanced for you at this stage. – Hovercraft Full Of Eels Sep 24 '16 at 17:57
  • @HovercraftFullOfEels Okay. Thanks for the help. I'll figure out how to do that. The question you linked to as duplicate is really helpful. – Face1ess74 Sep 24 '16 at 18:42
  • @AndrewThompson: May I ask, why is extending JPanel considered bad in this case? Is it because the panels do not contain much and could be directly implemented in the main-panel / frame? Or is there another reason? – hamena314 Sep 26 '16 at 08:04
  • When you use a button, do you extend button? When you use a menu, or menu items, do you extend each? I'm guessing the answer is 'no'. So answer me this, ***why*** extend a panel, or a frame? I can think of a reason we might need to extend a panel - custom painting. But none of those panels are custom painted, so ..why extend them? – Andrew Thompson Sep 26 '16 at 10:06

0 Answers0