0

I am a beginner programmer so this is a beginner question/project. I have a class called Screen which is a simple GUI made with a JTabbedPane and all of the tabs are blank. In the first tab, "actionPanel", I have a simple JLabel that contains the text "THIS IS A TEST." This JLabel is called dA, and there is a second empty one called dB.

I also have a class, Character, for an object with parameters name, HP, and a URL for the location of that character's corresponding image.

I want to be able to call a method, setupDialogueEnvironment(), which so far is only supposed to change the JLabel created by Screen to contain a character's corresponding image instead of "THIS IS A TEST." However, I keep getting null pointer exceptions when I try to do this. I believe it has something to do with the java.net.URL's I am using, or the ImageIcons perhaps. I am not sure. I know that the JLabel does appear when it just says "THIS IS A TEST", soo when it has nothing to do with images.

I wonder if I am not using the repaint() method correctly. You will see in my code that I tried calling repaint() on just about every JComponent I could to see if it would help, but it does not. My screen, after calling the method setupDialogueEnvironment(), still just displays "THIS IS A TEST."

Thank you in advance for help. Here is my code, and I will clarify a couple of points about the code below.


package Package1;
import javax.swing.*;
import java.awt.*;
import java.net.URL.*;
public class Screen {

    // Global declaration of GUI elements
    JFrame frame;
    static JPanel actionPanel, onPerson, wt, mats, craft, quests, notes;
    static JTabbedPane mtp, inventory;

    // Image URLs and their corresponding ImageIcons and JLabels
    static JLabel dA = new JLabel("THIS IS A TEST");
    static JLabel dB = new JLabel() ; // dialogue member A's and B's JLabels
    static ImageIcon diA = new ImageIcon();
    static ImageIcon diB = new ImageIcon(); // ImageIcons for dialogue members A and B

    public Screen()
    {
        // Set up the frame

        // Create tabbed panes and panels

        // Add tabs to JTabbedPanes

        // Finalize some JFrame things, like setDefaultCloseOperation

    }

    // the method in question
    public void setupDialogueEnvironment(URL urla, URL urlb)
    {
        diA = new ImageIcon(urla);
        diB = new ImageIcon(urlb);

        dA.setBackground(Color.RED);
        dB.setBackground(Color.BLUE);

        dA = new JLabel(diA);
        dB = new JLabel(diB);

        actionPanel.add(dA);
        actionPanel.add(dB);

        dA.setSize(300, 300);
        dB.setSize(300, 300);

        dA.setLocation(100, 100);
        dB.setLocation(600, 100);

        dA.setVisible(true);
        dB.setVisible(true);

        //repaint-- how do I get it to work?
        frame.repaint();
        actionPanel.repaint();
        mtp.repaint();
        dA.repaint();
        dB.repaint();

    }
}

  • I have a class called HomesteadRunner which contains my main method and creates a Screen object
  • In HomesteadRunner I have an object DialogueState, which calls a method whose only purpose is to call setupDialogueEnvironment() on the Screen object.
  • I have made two Character objects, each with a correctly copied file path for their corresponding image. The code for this is below.

    import package1.*;
    public class HomesteadRunner {
    public static void main(String[] args)
    {
        // Create characters and attack. See if HP adjusts properly.
        Skills skiller = new Skills();
    
        Screen screen = new Screen();
    
        Character ca = new Character("Bob", 100, Screen.class.getResource("C:\\Users\\jleekas\\IdeaProjects\\HomesteadV1\\src\\Resources\\imgres.jpg"));
        Character cb = new Character("Billy", 100, Screen.class.getResource("C:\\Users\\jleekas\\IdeaProjects\\HomesteadV1\\src\\Resources\\imgres-1.jpg"));
    
        // Create a dialogue state
        DialogueState ds = new DialogueState();
        ds.setup(ca, cb, screen);
    }
    

    }

Thank you very much for your help & advice in advance.

  • 3
    The heuristic for NullPointerExceptions is almost always the same. **You should critically read your exception's stacktrace to find the line of code at fault, the line that throws the exception, and then inspect that line carefully**, find out which variable is null, and then trace back into your code to see why. You will run into these again and again, trust me. In the future, please search on the subject before posting, since this is too common a problem to post yet another NPE question. – Hovercraft Full Of Eels Jan 06 '16 at 22:18
  • 1
    See also [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it/) – Andy Thomas Jan 06 '16 at 22:21
  • 2
    I don't see anywhere in your code where you try to set the label's icon. Also, why are you using static variables? – FredK Jan 06 '16 at 22:25
  • Static, static, static. You should avoid using static, especially for cross communication between classes – MadProgrammer Jan 06 '16 at 23:27

0 Answers0