0

I want to make a popup box where in each box there is a different message. I do not know the number of messages. I want to know what would be the best way to display those jFrames. I have used an array where the length of the array is the number of messages. The problem is that I am getting a NullPointerExeption. What am I doing wrong?

    public void interpret() {
    String[] command = html.split(";");

    for (int i = 0; i < command.length; i++) {
        //  System.out.println(command[i]);
        if (command[i].contains("message")) {
            showMessage(command[i].substring(8, command[i].length() - 1));
        }
    }



}
messagePopUp[] mes = new messagePopUp[10]; // I am just using length 10 for debugging

private void showMessage(String line) {

    mes[0].setTextAlert(line); // line giving me the error
    mes[0].setVisible(true);

}

The messagePopUp.class is just a default jPanel class which I have added the setTextAlert();

Thank you

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • *"Using an array of jFrames?"* See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) – Andrew Thompson Apr 20 '12 at 00:49

2 Answers2

3

Couldn't you use a JOptionPane? They are designed just to show dialogs and messages.

Jack
  • 131,802
  • 30
  • 241
  • 343
  • 1
    I agree with you that JOptionPane should be use instead of JFrames, but it's not the question aksed :D – Jason Rogers Apr 20 '12 at 00:24
  • @JasonRogers +1 for your answer, but see also.. [Is “Don't do it” a valid answer?](http://meta.stackexchange.com/questions/8891/is-dont-do-it-a-valid-answer) Using a `JOptionPane` would probably negate the need for an array at all, so side-steps the problem. :) – Andrew Thompson Apr 20 '12 at 00:53
  • I think it's a matter of taste and situation, it's hard to decide if it's a good way of answering or not. In this situation I just thought that negating the need of an array when an array is not needed is just a good solution :) But I understand your point of view. – Jack Apr 20 '12 at 01:06
2
messagePopUp[] mes = new messagePopUp[10];

this allocated an array of 10 messagePopup

but doesn't create 10 messagePopup objects ^^

you need to create the objects and store them in the array

I would use something like

private void showMessage(String line, int i) {
   if(mes[i]==null){
      mes[i] = new messagePopUp();
   }
   mes[i].setTextAlert(line); // line giving me the error
   mes[i].setVisible(true);
}

I also agree with @Jack, for what you currently doing JOptionPane seems more adapted to your needs

http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html

Jason Rogers
  • 19,194
  • 27
  • 79
  • 112