mardi 28 juin 2016

How to make an array from a class defined inside another class

I am a novice Java programmer trying to use classes defined in a different file. So, I've written these two .java files:

First, there's MyLibrary.java:

package mymainprogram;

public class MyLibrary {
    public class MyRecord {
        int number;
        char letter;
    }

    public static int TriplePlusThree(int input_number) {            
        return ((input_number*3) + 3);
    }
}

Then, MyMainProgram.java:

package mymainprogram;

import java.util.Scanner;

public class MyMainProgram {
    public static void main(String[] args) {
        Scanner keyread = new Scanner(System.in);

        System.out.print("Enter Number to Process: ");
        int num = keyread.nextInt();  
        int result = MyLibrary.TriplePlusThree(num);
        System.out.println("3x + 3 = "+result);

        String letters = "ABCDEFGHIJ";
        MyLibrary.MyRecord[] TenRecs = new MyLibrary.MyRecord[10];

        for (int i = 0; i < 10; i++) {
            TenRecs[i].number = i;      //NullPointerException here
            TenRecs[i].letter = letters.charAt(i);           
        }
    }
}

I had no problem getting the method to work just fine; now my goal is to create an array where each member of the array has an integer and character. (Note: I'm not looking for better ways to accomplish this objective; I'm merely using this trivial example to try to get this working).

When I tried to run my program, I got:

java.lang.NullPointerException

I researched this, and found this page, which says:

If we try to access the objects even before creating them, run time errors would occur. For instance, the following statement throws a NullPointerException during runtime which indicates that [this array] isn't yet pointing to [an] object. The objects have to be instantiated using the constructor of the class and their references should be assigned to the array elements in the following way.

studentArray[0] = new Student();

So, I tried to do that in my Main Program:

MyRecordArray[0] = new MyLibrary.MyRecord();

but that gives this error:

an enclosing instance that contains MyLibrary.MyRecord is required

That error message led me to this Stack Exchange question, which says:

you have to create an object of X class (outer class) and then use objX.new InnerClass() syntax to create an object of Y class.

X x   = new X();
X.Y y = x.new Y();

So, in accordance with that answer, I've added these two lines to my program:

MyLibrary mylibrary         = new MyLibrary();
MyLibrary.MyRecord myrecord = mylibrary.new MyRecord();

Those lines don't give any warnings or compilation errors, so I feel like I'm one step closer, but I'm still trying to figure out how to make an array. I know if I wanted to make an array of integers, I would simply do this:

int[] TenInts = new int[10];

So, I've tried things like:

myrecord[] TenRecs = new myrecord[10];
MyRecord[] TenRecs = new MyRecord[10];

But nothing is working, and I feel like I'm grasping at straws now. I get the feeling that the right set of eyes could solve this pretty quickly.

Aucun commentaire:

Enregistrer un commentaire