0

Im trying to create a program that allows me to change the the variables R,G,B in the Text class. Whenever I try to run the Applet, by clicking the "SubmitR" Button, it gives me a Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException.

public class Main extends JApplet {
private ButtonHandlerR buttonHandlerR;




@Override
public void start() {
    super.start();
}


@Override
public void init() {


    this.setSize(750, 300);

    setLayout(new BorderLayout());

    add(new SetTextColour(), BorderLayout.NORTH);
    add(new Text(),BorderLayout.CENTER);






}

}

public class SetTextColour extends JPanel {


private JLabel labelR;
private JLabel labelG;
private JLabel labelB;

public JTextField textR;
public JTextField textG;
public JTextField textB;

public JButton submitR;
public JButton submitG;
public JButton submitB;


public SetTextColour() {

    labelR = new JLabel("RED: ");
    labelG = new JLabel("GREEN: ");
    labelB = new JLabel("BLUE: ");

    textR = new JTextField(10);
    textB = new JTextField(10);
    textG = new JTextField(10);


    add(textR, BorderLayout.NORTH);
    submitR = new JButton("SubmitR");
    add(submitR, BorderLayout.NORTH);

    add(textB, BorderLayout.NORTH);

    submitG = new JButton("SubmitG");
    add(submitG, BorderLayout.NORTH);

    add(textG, BorderLayout.NORTH);

    submitB = new JButton("SubmitB");
    add(submitB, BorderLayout.NORTH);

    ButtonHandlerB BHB = new ButtonHandlerB();
    ButtonHandlerG BHG = new ButtonHandlerG();
    ButtonHandlerR BHR = new ButtonHandlerR(this);

    submitB.addActionListener(BHB);
    submitR.addActionListener(BHR);
    submitG.addActionListener(BHG);


}

}

public class ButtonHandlerR implements ActionListener {
private SetTextColour colour;
private Text text;


ButtonHandlerR(Text change){
    this.text = change;

}

ButtonHandlerR(SetTextColour set){
    this.colour = set;

}






@Override
public void actionPerformed(ActionEvent e) {

    JButton Clicked = (JButton) e.getSource();
    double tempV;
    int tempV2;

    if(colour.submitR == Clicked){
        tempV = Double.parseDouble(colour.textR.getText());
        tempV2 = (int) tempV;
        text.R = tempV2;
        System.out.println(text.R);
        text.repaint();


    }



}

}

public class Text extends JApplet {
private String textField = "Welcome to CE203 Assignment 1 - Hassan Khan, 1404460";
 public int R=50;
 private int G=32;
 private int B=54;

public void start(){

}





public void init(){




}

public void paint (Graphics g) {


    Color customColor = new Color(R, G, B);

    g.setColor(customColor);

    g.drawString(textField, 125, 150);



}

}

Khan
  • 1
  • 1
    You have 2 classes that extend `JApplet`, something's fishy there. You also have 2 constructors for `ButtonHandlerR`, again, not right. `new SetTextColour()` and `new Text()` have no knowledge of each other, since you never pass the reference from to the other. No way this is gonna work. Better start with baby steps and learn how Java works. – Guillaume Polet Jul 20 '16 at 20:56
  • As far as changing colors go, see `JFileChooser` General tips: 1) See [Detection/fix for the hanging close bracket of a code block](http://meta.stackexchange.com/q/251795/155831) for a problem I could no longer be bothered fixing. 2) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). .. – Andrew Thompson Jul 21 '16 at 05:40
  • .. 3) See [Java Plugin support deprecated](http://www.gizmodo.com.au/2016/01/rest-in-hell-java-plug-in/) and [Moving to a Plugin-Free Web](https://blogs.oracle.com/java-platform-group/entry/moving_to_a_plugin_free). – Andrew Thompson Jul 21 '16 at 05:40

1 Answers1

1

Shouldn't you use SwingUtilities.invokeLater or SwingUtilities.invokeAndWait here ?

I haven't used swing for some time, but this is my memory. Can you try it out: https://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html

Alexander Petrov
  • 9,204
  • 31
  • 70