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.