Results 1 to 8 of 8
  1. #1
    Steffers is offline Member
    Join Date
    Nov 2010
    Posts
    4
    Rep Power
    0

    Default Won't set to textfield

    I'm creating an email interface using an arraylist and filehandling. The idea is that the user can select a line in the display table and that message will be displayed when they click view. I've gotten as far as setting the text in textfields on the view screen but it won't let me set the information from the array. The weird thing is that it will print out into the command prompt. Otherwise I get a nullpointerexception. Can anyone help?! Thanks in advance.

    public void setToScreen(){
    System.out.println(mail.get(0).getTo());
    textField.setText(mail.get(row).getTo());

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,013
    Rep Power
    10

    Default

    Well, you're printing out two completely different things there: whatever is in the 0th index, and whatever is in the row index. What is row? Use a debugger, or at least some print statements, to figure out what's going on there.

  3. #3
    Steffers is offline Member
    Join Date
    Nov 2010
    Posts
    4
    Rep Power
    0

    Default

    Sorry, row is a variable that contains an int. I'm using Netbeans and I think there's a probkem with the generated code. Thanks for your help.

  4. #4
    Steffers is offline Member
    Join Date
    Nov 2010
    Posts
    4
    Rep Power
    0

    Default Still doesn't work

    I've tried more things and I still can't get it to work. Am I just missing something? Sorry if my code is a bit unclear.

    public void displayOnScreen(){

    //to test if the data is being passed properly
    System.out.println(mail.get(row).getTo());
    System.out.println(mail.get(row).getFrom());
    System.out.println(mail.get(row).getSubject());
    System.out.println(mail.get(row).getDate());
    System.out.println(mail.get(row).getMessage());

    //To assign the value from mail (arraylist) to a string (just in case). Row is an int value.
    to = mail.get(row).getTo().toString();
    from = mail.get(row).getFrom().toString();
    subject = mail.get(row).getSubject().toString();
    date = mail.get(row).getDate().toString();
    message = mail.get(row).getMessage().toString();

    //Setting the string to the textfield which doesn't work
    toField.setText(to.toString());
    fromField.setText(from.toString());
    System.out.println("Setting");
    subjectField.setText(subject.toString());
    dateField.setText(date.toString());
    body.setText(message.toString());
    }
    }

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,013
    Rep Power
    10

    Default

    I assumed row was an int. The question you need to figure out is, what value does row have when it's being passed in that causes the exception? Throw some print statements in there. Also print out the size of the ArrayList (or whatever mail is). Or better yet, use a debugger.

    Honestly, most people here (myself included) would advise you to ditch the IDE-generated code and do it yourself. You'll save yourself (and other people trying to help you) a ton of headaches if you do so.

    Code you post should be in the form of an SSCCE, and you should always use the code tags to preserve formatting.

  6. #6
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    307
    Rep Power
    6

    Default

    Have you tracked down what is the null that is throwing the error?

    Add debug lines before you set the text to the textfield that displays the text you are trying to set and the textfield you are trying to set it to. I don't see constructor calls for the JTextField's and since you can set a null to them, its more likely that the JTextField hasn't been initialized properly or you aren't keeping the reference to it.

  7. #7
    Steffers is offline Member
    Join Date
    Nov 2010
    Posts
    4
    Rep Power
    0

    Default

    I ditched the generated code as you suggested and created my own but the same thing is happening. Nothing is displaying in the textfields. It no longer throws a nullpointerexception though and using print statements I found out that the proper information is being passed through. I have also made sure that the textfields are properly declared and created. Thanks to you both for your help on this.

  8. #8
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,013
    Rep Power
    10

    Default

    Well, that's a step in the right direction. If you want more help, I'd definitely suggest you post that SSCCE.

Similar Threads

  1. set focus to the textfield
    By paty in forum New To Java
    Replies: 2
    Last Post: 07-09-2010, 09:28 AM
  2. 1 question regarding textfield
    By henry in forum New To Java
    Replies: 3
    Last Post: 06-16-2010, 04:56 AM
  3. TextField Example
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-25-2008, 03:21 PM
  4. [SOLVED] Textfield
    By Fireking in forum New To Java
    Replies: 2
    Last Post: 07-11-2008, 04:40 AM
  5. JSP - getting value from a textfield
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-01-2007, 09:58 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •