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

1

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

3 Comments

I agree with you that JOptionPane should be use instead of JFrames, but it's not the question aksed :D
@JasonRogers +1 for your answer, but see also.. Is “Don't do it” a valid answer? Using a JOptionPane would probably negate the need for an array at all, so side-steps the problem. :)
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.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.