Results 1 to 9 of 9
  1. #1
    EBBOOO is offline Member
    Join Date
    Jun 2010
    Posts
    42
    Rep Power
    0

    Question what is wrong with my code?? URGENT, i have to finish this tonight :S

    let me tell you about my program first, the program is to ask the user to enter a number of students. then the same number of textfields must be displayed. the user has to enter students names, and grades for 6 subjects.
    when i run this code and add the number of students, the textfields doesn't show up, i don't know why :confused:
    by the way, I'm a beginner so there is alot of things that i don't know yet :o
    =================================================

    Java Code:
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    
    public class students_grades extends Applet implements ActionListener {
        Label title = new Label("<{This program calculate students grades in 6 subjects}>");
        Label number_of_students = new Label("<{Inter the number of the students}>");
        TextField number_of_students_ = new TextField(5);
        Button View_Table = new Button ("View Table");
    
        
    public void init( )  {
        //setLayout();
            add (title);
            add (number_of_students);
            add (number_of_students_);
            add (View_Table);
            number_of_students_.addActionListener(this);
            View_Table.addActionListener(this);
           
        }   
    public void actionPerformed(ActionEvent event) {
       
        // to get the number of students entered by user
       //         String titles [8] = [No. , Student Name, Math, Comp, Salam, Arab, MGT, ORG];
        String number_of_students_text = number_of_students_.getText();
        int number_of_students_int = Integer.parseInt(number_of_students_text);
        
           // define two arrays of textfield
        TextField txtname []= new TextField [number_of_students_int];
        TextField txtgrade [][] = new TextField [number_of_students_int][6];
        
        // define two arrays ===> one for student names, and the other one for students grade
        String students_names [] = new String [number_of_students_int];
        float students_grades [][] = new float [number_of_students_int][6];
        
        // add name text fields
        for (int i=0; i<number_of_students_int; i++)
        {
            this.add (txtname[i]);
        }
        // add grade text fields in 2D array
        for (int i=0; i<number_of_students_int ;i++)
        {
            for (int j=0; j<6 ; j++)
            {
                add (txtgrade[i][j]);
            }
        }
        
        // get text from text fields and save it in the array
        for (int i=0; i<number_of_students_int; i++)
        {
            students_names [i] = txtname[i].getText();
        }
        
        for (int i=0; i<number_of_students_int ;i++)
        {
            for (int j=0; j<6 ; j++)
            {
                String Container = txtgrade[i][j].getText();
                int intContainer = Integer.parseInt(Container);
                students_grades[i][j] = intContainer;
                }
        }
        
        
    } // action performed
    } // class
    Moderator Edit: Code tags added
    Last edited by Fubarable; 06-05-2010 at 06:53 PM. Reason: Moderator Edit: Code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    OP, I edited your code and added code tags which should help make your posted code retain its formatting and be more readable.

    To do this yourself, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

    Another way to do this is to manually place the tags into your code by placing the tag [cod&#101;] above your pasted code and the tag [/cod&#101;] below your pasted code like so:

    Java Code:
    [cod&#101;]
      // your code goes here
      // notice how the top and bottom tags are different
    [/cod&#101;]
    Best of luck

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,514
    Rep Power
    25

    Default

    What happens when you execute it? Do you get any error messages in the browser's Java console? Please copy and paste them here if you need help.

  4. #4
    EBBOOO is offline Member
    Join Date
    Jun 2010
    Posts
    42
    Rep Power
    0

    Default

    can you see the codes now?
    Java Code:
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    
    public class students_grades extends Applet implements ActionListener {
        Label title = new Label("<{This program calculate students grades in 6 subjects}>");
        Label number_of_students = new Label("<{Inter the number of the students}>");
        TextField number_of_students_ = new TextField(5);
        Button View_Table = new Button ("View Table");
    
        
    public void init( )  {
        //setLayout();
            add (title);
            add (number_of_students);
            add (number_of_students_);
            add (View_Table);
            number_of_students_.addActionListener(this);
            View_Table.addActionListener(this);
           
        }   
    public void actionPerformed(ActionEvent event) {
       
        // to get the number of students entered by user
       //         String titles [8] = [No. , Student Name, Math, Comp, Salam, Arab, MGT, ORG];
        String number_of_students_text = number_of_students_.getText();
        int number_of_students_int = Integer.parseInt(number_of_students_text);
        
           // define two arrays of textfield
        TextField txtname []= new TextField [number_of_students_int];
        TextField txtgrade [][] = new TextField [number_of_students_int][6];
        
        // define two arrays ===> one for student names, and the other one for students grade
        String students_names [] = new String [number_of_students_int];
        float students_grades [][] = new float [number_of_students_int][6];
        
        // add name text fields
        for (int i=0; i<number_of_students_int; i++)
        {
            this.add (txtname[i]);
        }
        // add grade text fields in 2D array
        for (int i=0; i<number_of_students_int ;i++)
        {
            for (int j=0; j<6 ; j++)
            {
                add (txtgrade[i][j]);
            }
        }
        
        // get text from text fields and save it in the array
        for (int i=0; i<number_of_students_int; i++)
        {
            students_names [i] = txtname[i].getText();
        }
        
        for (int i=0; i<number_of_students_int ;i++)
        {
            for (int j=0; j<6 ; j++)
            {
                String Container = txtgrade[i][j].getText();
                int intContainer = Integer.parseInt(Container);
                students_grades[i][j] = intContainer;
                }
        }
        
        
    } // action performed
    } // class

  5. #5
    EBBOOO is offline Member
    Join Date
    Jun 2010
    Posts
    42
    Rep Power
    0

    Default

    no there is no errors
    when i execute it and add the number of the students and press the button, nothing happen!!!!

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,514
    Rep Power
    25

    Default

    Are you sure? When I execute it I get Null Pointer exceptions.

    Comments:
    Defining an array does not put values into the elements of the array. You must do that for every element in the array.
    TextField[] tf = new TextField[10]; // defines an array of empty slots
    tf[0] = new TextField(); // fill slot 0 the the array with ref to a tf

    Your code gets into some advanced layout management. You add components to the applet display and then wait for user input. When you get the input, you want to change the display. To do that you must use the removeAll() method to get rid of the old stuff,
    add the new stuff and then call validate() to tell the system to do another layout of your components.


    You'll need a new button for the user to use AFTER he has filled in the fields so you can get the data he has entered.

  7. #7
    EBBOOO is offline Member
    Join Date
    Jun 2010
    Posts
    42
    Rep Power
    0

    Default

    i defined the array as you said, but it shows error in the array!!

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,514
    Rep Power
    25

    Default

    Sorry, I don't have my long range glasses on. I can't see either the code or the error.
    You'll have to post both of them.

  9. #9
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by EBBOOO View Post
    TextField txtgrade [][] = new TextField[number_of_students_int][6];

    for (int j=0; j<6 ; j++)
    {
    add (txtgrade[i][j]);
    }

    with the code above, you create arrays of TextField, but the reference of the array is pointing to null! in order to fix that use

    Java Code:
            for (int j=0; j<6 ; j++)
            {
            	TextField gradefield = new TextField();
            	txtgrade[i][j] = gradefield;
                add (txtgrade[i][j]);
            }

    now, each element of the array is referencing an object of type TextField. apply the same to the other TextFields and for the layout of these fields i would use a gridlayout.

Similar Threads

  1. Assignment due tonight, please help me!
    By twiggy62 in forum New To Java
    Replies: 4
    Last Post: 02-09-2010, 06:07 AM
  2. What's going wrong with this code?
    By Suurisa in forum New To Java
    Replies: 5
    Last Post: 10-19-2009, 11:59 PM
  3. what is wrong in dis code?
    By jitun2004 in forum New To Java
    Replies: 8
    Last Post: 04-15-2009, 09:30 AM
  4. What is wrong with this code
    By rosh72851 in forum New To Java
    Replies: 13
    Last Post: 10-31-2008, 01:50 AM
  5. what is wrong with this code
    By masaka in forum New To Java
    Replies: 5
    Last Post: 04-16-2008, 08:27 AM

Posting Permissions

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