int cannot be dereferenced error message

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JPane
    New Member
    • Feb 2008
    • 6

    int cannot be dereferenced error message

    [CODE=Java]package bankobject;
    import java.io.*;
    import javax.swing.JOp tionPane;

    class Number {
    String studentName;
    String teacherName;
    int[] Number;

    public Number () {
    studentName = JOptionPane.sho wInputDialog("P lease Enter student name");
    teacherName = JOptionPane.sho wInputDialog("P lease Enter the teacher name ");
    int numNumber = Integer.parseIn t(JOptionPane.s howInputDialog( "Please enter the number of numbers"));
    Number[] obj = new Number [numNumber];
    for (i = 0; i < numNumber.lengt h; i++)
    obj [i] = new Number ();
    }
    [/CODE]

    I want the array to take as many numbers object, by using a for loop and instantiate each number object by calling the Number constructor.
    Last edited by BigDaddyLH; Feb 15 '08, 04:46 AM. Reason: Added Java to code tag
  • BigDaddyLH
    Recognized Expert Top Contributor
    • Dec 2007
    • 1216

    #2
    I think you meant to write this:

    [CODE=Java]for (int i = 0; i < numNumber; i++)[/CODE]

    Comment

    • BigDaddyLH
      Recognized Expert Top Contributor
      • Dec 2007
      • 1216

      #3
      There are also several logical errors in your code that you need to attend to. In your Number class you create an array full of Number objects -- that could lead to an infinite loop if you keep creating non-empty arrays. I think you meant the user interaction code to be elsewhere, perhaps in the main method, which hasn't shown up, yet. Also you have an int array called Number -- surely pure confusion on your part?

      Comment

      • JPane
        New Member
        • Feb 2008
        • 6

        #4
        Originally posted by BigDaddyLH
        There are also several logical errors in your code that you need to attend to. In your Number class you create an array full of Number objects -- that could lead to an infinite loop if you keep creating non-empty arrays. I think you meant the user interaction code to be elsewhere, perhaps in the main method, which hasn't shown up, yet. Also you have an int array called Number -- surely pure confusion on your part?
        Yeah, I meant for (int i = 0; i < numNumber; i++); forget the int there.

        in the attributes list I wanted to make an array of number objects then in the constructor I would prompt the user for the number of number objects and use a for loop to instantiate each number account using the number constructor for each.

        in the main method I would just instantiate two number objects.

        The problem I'm having is in the for loop and getting the error " int cannot be dereferenced error message" I know I have to do something with it because it's a primitive, just don't know how in an array and for loop.

        Comment

        • Ganon11
          Recognized Expert Specialist
          • Oct 2006
          • 3651

          #5
          Don't know if this will fix your problem, but it looks like you declare a private data member of the Number class called int[] Number - that;s bound to give you problems. Consider a better name for it.

          Comment

          • JPane
            New Member
            • Feb 2008
            • 6

            #6
            Originally posted by Ganon11
            Don't know if this will fix your problem, but it looks like you declare a private data member of the Number class called int[] Number - that;s bound to give you problems. Consider a better name for it.

            maybe int [] grade is better.


            Still doesn't work since the error is coming with this line

            for (int i = 0; i < numNumber; i++)

            Comment

            • BigDaddyLH
              Recognized Expert Top Contributor
              • Dec 2007
              • 1216

              #7
              I don't get syntax errors from your code when I change it as I indicated.

              Comment

              • JPane
                New Member
                • Feb 2008
                • 6

                #8
                Thanks for the help.

                Comment

                • JPane
                  New Member
                  • Feb 2008
                  • 6

                  #9
                  Originally posted by BigDaddyLH
                  There are also several logical errors in your code that you need to attend to. In your Number class you create an array full of Number objects -- that could lead to an infinite loop if you keep creating non-empty arrays. I think you meant the user interaction code to be elsewhere, perhaps in the main method, which hasn't shown up, yet. Also you have an int array called Number -- surely pure confusion on your part?
                  If I have two classes which are number and student how would I call them up in the main method when I just instantiate a number object. Meaning the input attributes in the student class.

                  Both classes are composition of each other.

                  Comment

                  • BigDaddyLH
                    Recognized Expert Top Contributor
                    • Dec 2007
                    • 1216

                    #10
                    Originally posted by JPane
                    If I have two classes which are number and student how would I call them up in the main method when I just instantiate a number object. Meaning the input attributes in the student class.

                    Both classes are composition of each other.
                    I don't understand what you are trying to say. perhaps is you posted some code to explain what you are trying to do.

                    Comment

                    • JPane
                      New Member
                      • Feb 2008
                      • 6

                      #11
                      Code:
                            package studentobject;
                      
                            import java.io.*;
                      
                            import javax.swing.JOptionPane;
                       
                      
                            class Number {
                       
                               String studentName;
                      
                      
                               int[] Number;
                      
                      
                            public Number () {
                       
                               studentName = JOptionPane.showInputDialog("Please Enter student name");
                        
                                teacherName = JOptionPane.showInputDialog("Please Enter the teacher name ");
                      
                                int numNumber = Integer.parseInt(JOptionPane.showInputDialog("Please enter the number of numbers"));
                      
                                Number[] obj = new Number [numNumber];
                        
                                for (i = 0; i < numNumber.length; i++)
                        
                                   obj [i] = new Number ();   
                      
                            }
                      
                      
                      class Student
                      
                      {
                          
                          String schoolName;
                          
                          
                      // Methods
                       
                         
                          void  students()
                                  
                          { 
                              
                              
                              double students = 0;
                              
                              
                          }
                         
                         
                          
                          // Constructor
                           public Student ()
                               
                       {
                      
                           
                         schoolName = JOptionPane.showInputDialog("Please Enter the school name ");
                      
                                                 
                       }
                          
                        }
                      
                      
                      }
                      public class StudentOject  {
                      
                       
                          public static void main(String[] args) {
                              
                            Number nu = new Number ();
                            
                             
                         String output = "Student Name " + nu.studentName + "      Teacher Name: "  + nu.teacherName
                               
                          
                                      
                                      
                                      
                                 
                       
                                    
                              
                              JOptionPane.showMessageDialog(null, output, "Student Information",
                                      JOptionPane.INFORMATION_MESSAGE);
                              
                              
                          }
                      
                      
                      }
                      Since the student class is part of the number class when I do the output it isn't going to work with the student class because I only made a new object in the number class.

                      Comment

                      • BigDaddyLH
                        Recognized Expert Top Contributor
                        • Dec 2007
                        • 1216

                        #12
                        Originally posted by JPane
                        [CODE=Java]
                        public Number () {
                        ...
                        int numNumber = Integer.parseIn t(JOptionPane.s howInputDialog( "Please enter the number of numbers"));
                        Number[] obj = new Number [numNumber];
                        for (i = 0; i < numNumber.lengt h; i++)
                        obj [i] = new Number ();
                        }
                        [/CODE]
                        I can't get past this constructor. Do you understand it? When you construct a Number object, you prompt the user for a size, allocate an array and then construct more Number objects, each of which will prompt the user for a size, allocate an array and then construct more Number objects, each of which will prompt the user for a size, allocate an array ...

                        I think you need to start over again. I still have no idea what you are trying to do, and I'm not convinced anyone does, including yourself. First, answer these questions.
                        1. What is a Number object? What are its key responsibilitie s?
                        2. What is a Student object? What are its key responsibilitie s?
                        3. What is a StudentObject object? What are its key responsibilitie s?

                        Comment

                        Working...