-
class array issue
If anyone can explain the compiler error below I'd be grateful and it would end hours of torment. I'm sure its simple but I can't see it. The highlighted error in Eclipse is in bold (line 25).
And any smooth way to initialise a class array when you do not know how many elements you want? I thought the method here would work but perhaps not.
Thanks in anticipation.
The error: No enclosing instance of type autodefcreate is accessible. Must qualify the allocation with an enclosing instance of type autodefcreate (e.g. x.new A() where x is an instance of autodefcreate).
at autodef.autodefcreate.main(autodefcreate.java:25)
Code:
package autodef;
import java.io.*;
public class autodefcreate {
String scriptname;
public static void main(String[] args) {
String scriptname;
// read *.def and store value, database and column name in arrays
try {
if (args[0] == null) {
scriptname = "fred";
}
else {
scriptname = args[0];
}
System.out.println(scriptname);
FileReader input = new FileReader(scriptname + ".def");
/* Filter FileReader through a Buffered read to read a line at a
time */
BufferedReader bufRead = new BufferedReader(input);
defvariable[] arraydef = new defvariable[5]; // array to hold def lines
arraydef[1] = new defvariable("AA", "BB", "CC");
String line; // String that holds current file line
int count = 0; // Line number of count
line = bufRead.readLine();
// Read through file one line at time. Print line # and line
while (line != null){
// split line and load components into array
String[] temp;
temp = line.split("\\|");
// if length = 4 then a valid def definition
if ( temp.length == 4 ) {
System.out.println(count); // zero
System.out.println(temp[1]); // 1234
System.out.println(temp[2]); // database1
System.out.println(temp[3]); // datacol1
System.out.println(arraydef[0].getDatabase());
count++;
}
line = bufRead.readLine();
}
bufRead.close();
}catch (IOException e){
// If another exception is generated, print a stack trace
e.printStackTrace();
}
try {
FileWriter outFile = new FileWriter("javawritefile");
// FileWriter outFile = new FileWriter(args[0]);
PrintWriter out = new PrintWriter(outFile);
// Also could be written as follows on one line
// Printwriter out = new PrintWriter(new FileWriter(args[0]));
// Write text to file
out.println("This is line 1");
out.println("This is line 2");
out.print("This is line3 part 1, ");
out.println("this is line 3 part 2");
out.close();
} catch (IOException e){
e.printStackTrace();
}
}
class defvariable {
String substitution;
String database;
String datacolumn;
public defvariable(String bit1, String bit2, String bit3) {
this.substitution = bit1;
this.database = bit2;
this.datacolumn = bit3;
}
public String getSubstitution() {
return substitution;
}
public void setSubstitution(String substitution) {
this.substitution = substitution;
}
public String getDatabase() {
return database;
}
public void setDatabase(String database) {
this.database = database;
}
public String getDatacolumn() {
return datacolumn;
}
public void setDatacolumn(String datacolumn) {
this.datacolumn = datacolumn;
}
}
}
-
Re: class array issue
The problem is that defvariable is an inner class of autodefcreate. This means that (as defvariable is non-static) you can only access defvariable from a non-static context, but the main() method is static.
There are two ways to fix this. Either declare defvariable to be static, or put defvariable in its own file.
-
Re: class array issue
Peter very many thanks for your help. All working now. Clearly I need to read up on my 'static's.
-
Re: class array issue
You need to read up on Inner Classes and while your at it may I suggest you look at the Java Naming Conventions as well. They are not compulsory standards but using them means it's generally easier for other people to read your code.
To be fair your code pretty much complies with the conventions apart from class names which should start with an upper case letter. Also using camel case can make names easier to read ie 'scriptName' instead of 'scriptname', 'AutoDefCreate' instead of 'autodefcreate'
-
Re: class array issue
keang I'll certainly do that. Just getting going (2 days) with assessing Java and eclipse but I like it enough that I should start to tidy up. Hopefully squeaky clean next time, Cheers