I was developing a program using NetBeans IDE and i got an error in front of a line saying
illegal start of expression and below that written ‘;’ expected
I am new to Java and I am unable to fix this error when I was assigning a value to an array.
Below is a part of code where the error occured :
String[] colname;
int j=0;
while(rs.next()){
for(int i=0;i<cols;i++){
colname={dtm.getColumnName(i)}; //**<-- This is where the error occured**
}
colName=colname; //colName is also an array of String datatype.
Object[] value = {rs.getObject(colName[j])};
dtm.addRow(value);
j++;
}
All apart the line
colname={dtm.getColumnName(i)};
Does not give any error. But the error occurs only in the above line.
I found myself unable to fix it. Can anyone help me to fix it?
You have 2 ways of initializing an array:
or
But you can’t mix them. In your case, you would use the latter because you don’t have the information to populate it yet on the line where you declare it.
Note however that this is probably not going to do what you want as you will keep reassigning a new array at each loop. You could make your life easier by using an ArrayList instead:
You can read more about arrays in this tutorial.