Java JTextField Array reading problems

problems reading JTextField array

Page 1 of 1

6 Replies - 1978 Views - Last Post: 17 December 2010 - 03:20 PM Rate Topic: -----

#1 Guest_Relax*


Reputation:

Java JTextField Array reading problems

Posted 17 December 2010 - 07:53 AM

private int x = 0;

public void addfields() {
        x++;
        JTextField tfserie[]=new JTextField[100];
        tfserie[x] = new JTextField(15);
        tfserie[x].setText(serie.getText());
        p.add(tfserie[x]);

        JTextField tfeps[]=new JTextField[100];
        tfeps[x] = new JTextField(5);
        tfeps[x].setText(eps.getText());
        p.add(tfeps[x]);
        System.out.println(tfserie[2].getText());
        



heres where the problem ocures:
        String data = "";
        for(int i=1;i<=x;i++) {
            data = data + tfserie[i].getText()+";";   <-----------invalid lines
            data = data + tfeps[i].getText()+";";  <--------------error here to
        }
       
       

    }


This post has been edited by macosxnerd101: 17 December 2010 - 01:17 PM
Reason for edit:: Please use code tags


Is This A Good Question/Topic? 0

Replies To: Java JTextField Array reading problems

#2 mi14chal  Icon User is offline

  • D.I.C Head

Reputation: 81
  • View blog
  • Posts: 202
  • Joined: 11-December 10

Re: Java JTextField Array reading problems

Posted 17 December 2010 - 08:17 AM

What are errors?
Was This Post Helpful? 0
  • +
  • -

#3 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 497
  • View blog
  • Posts: 1,453
  • Joined: 19-October 09

Re: Java JTextField Array reading problems

Posted 17 December 2010 - 08:34 AM

What error are you getting? You know you can do this data+= instead of data = data +.
Was This Post Helpful? 0
  • +
  • -

#4 Guest_Relax*


Reputation:

Re: Java JTextField Array reading problems

Posted 17 December 2010 - 08:39 AM

the error iam getting is
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

You know you can do this data+= instead of data = data +

dident know that thx



the code is the code from pressing a button that takes the text from 2fields and put them in the new one that the function creates
Was This Post Helpful? 0

#5 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 497
  • View blog
  • Posts: 1,453
  • Joined: 19-October 09

Re: Java JTextField Array reading problems

Posted 17 December 2010 - 01:15 PM

Could you post the whole of your code rather than a snippet, this will help us identify where abouts you are getting a NullPointerException.

Post your code using these tags below.

Posted Image
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10824
  • View blog
  • Posts: 40,358
  • Joined: 27-December 08

Re: Java JTextField Array reading problems

Posted 17 December 2010 - 01:18 PM

You are creating 100 JTextField variables, all of them null. You have to instantiate them to point to new JTextFields.
JTextField tfeps[]=new JTextField[100];


Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: Java JTextField Array reading problems

Posted 17 December 2010 - 03:20 PM

You are overloading your array at each call
private int x = 0;
JTextField tfserie[]=new JTextField[100];    // only once

public void addfields() {
//        x++;
        tfserie[x] = new JTextField(15);
        tfserie[x].setText(serie.getText());
        p.add(tfserie[x]);
        x++;    // here


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1