Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: problem in 2D array of TextField

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Exclamation problem in 2D array of TextField

    I have a problem in this code.I can't display the array of Text Field.

    import javax.swing.*;
    import java.awt.*;
     
    public class Projct extends JFrame {
        int x=3;
     
    public Projct(){
     
        Container c=getContentPane();
        c.setLayout(new BorderLayout());
     
        JPanel p=new JPanel();
        //new GridLayout(x,x)
        c.add(p,BorderLayout.CENTER);
    JTextField [][] array=new JTextField[x][x];
        for(int i=0;i<x;i++){
             for(int j=0;j<x;j++){
     
               array[i][j].setText(" ");
     
                 p.add(array[i][j]);
     
     
     
    }
        }
     
    int m=x;
     double [][] data=new double [m][m];
     
         for(int i=0;i<m;i++){
             for(int j=0;j<m;j++){
                 if (i == j) {
                 data[i][j]=1;
              array[i][j].setText("1");
             array[i][j].setEnabled(false);
     
                 }
        else{
     
     
                data[i][j]=Double.parseDouble(array[i][j].getText());
                  data[j][i]=(1/data[i][j]);
                 }}}
     
     
     
        }
     
    public static void main(String[]arg){
        Projct f=new Projct();
        f.setTitle("AHP");
        f.setVisible(true);
        f.setSize(500, 500);
        f.setLocation(40,60);
       f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    }
     
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: problem in 2D array of TextField

    Define can't display...does it compile? Are there any exceptions? (hint: see Common Java Mistakes)

  3. #3
    Junior Member
    Join Date
    Jan 2011
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: problem in 2D array of TextField

    Quote Originally Posted by copeg View Post
    Define can't display...does it compile? Are there any exceptions? (hint: see Common Java Mistakes)

    no it doesn't compile

    Exception in thread "main" java.lang.NullPointerException
    at Projct.<init>(Projct.java:20)
    at Projct.main(Projct.java:52)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 1 second)




    i don't know the order of writing the arrays
    1st array is array of Text Field
    2nd array is array of values that will enter by user

    I want the result to be like this pic

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: problem in 2D array of TextField

    no it doesn't compile

    Exception in thread "main" java.lang.NullPointerException
    Based upon that it sure looks like it compiles...a NullPointerException is a runtime exception . Did you have a look at the link I posted above? You've created an array without initializing its elements.
    Last edited by copeg; January 6th, 2011 at 08:18 PM.

  5. The Following User Says Thank You to copeg For This Useful Post:

    Eng.muslima (January 6th, 2011)

  6. #5
    Junior Member
    Join Date
    Jan 2011
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: problem in 2D array of TextField

    Quote Originally Posted by copeg View Post
    Based upon that it sure looks like it compiles...a NullPointerException is a runtime exception . Did you have a look at the link I posted above? You've created an array without initializing its elements.
    thanks for this information.

    I read
    Problem description: Forgetting to initialize a variable
    Problem description: Array Indices/Off by 1 errors

    but my problem is how can I initialize the variable of 2D array
    I want blank text fields then the user only type the values of upper triangle then the program will assign this values to the inverted triangle , diagonal will be always = 1 by setTextField

    I have an exam & I want to find the solve

  7. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: problem in 2D array of TextField

    but my problem is how can I initialize the variable of 2D array
    Do you mean something like this?
    String[] strings = new String[1];
    int len = strings[0].length();//NullPointerException, the value strings[0] was never initialized
    string[0] = new String("Testing");
    len = strings[0].length();//Now it has been initialized
    The same principle holds true for all objects. Nowhere in the code you posted above is there a "new JTextField()", so all the members of the array will be null until they are instantiated.
    Last edited by copeg; January 6th, 2011 at 09:07 PM.

  8. #7
    Junior Member
    Join Date
    Jan 2011
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: problem in 2D array of TextField

    Thank you very much
    array[ ][ ] is work and the frame is appear , now I have to complete my code then data [ ] [ ] will work

Similar Threads

  1. Populating a 2D array with textfield values
    By drixnak in forum What's Wrong With My Code?
    Replies: 9
    Last Post: October 19th, 2010, 01:20 PM
  2. ComboBox and TextField
    By fahad in forum AWT / Java Swing
    Replies: 2
    Last Post: May 31st, 2010, 08:47 AM
  3. Need Help with my JComboBox and Textfield
    By superawesome in forum AWT / Java Swing
    Replies: 1
    Last Post: November 10th, 2009, 12:15 PM
  4. JComboBox and Textfield
    By Nexusfactor in forum AWT / Java Swing
    Replies: 3
    Last Post: November 9th, 2009, 12:57 PM
  5. Combo/TextField Help
    By GRossi0511 in forum AWT / Java Swing
    Replies: 2
    Last Post: October 26th, 2009, 08:15 PM