Click to See Complete Forum and Search --> : Is import not working?
abudabit
01-29-2005, 06:31 PM
When I try to compile my script I get this:
C:\robot>javac practice.java
practice.java:30: cannot resolve symbol
symbol : method random ()
location: class practice
Robots[i] = round(random() * 1000);
^
practice.java:30: cannot resolve symbol
symbol : method round (int)
location: class practice
Robots[i] = round(random() * 1000);
^
practice.java:34: cannot resolve symbol
symbol : method random ()
location: class practice
PLine = (random() * 10);
^
3 errors
C:\robot>
However I have this line at the very beggining of my script:
import java.lang.Math;
Does this mean that my import isn't working?
abudabit
01-29-2005, 06:48 PM
Nevermind, figured it out. Should have used Math.random and such. I can't seem to delete the post. Sorry.
abudabit
01-29-2005, 07:18 PM
Sorry about all the questions, just getting started with Java. All I know is PHP at the moment.
Anyways, I finally compiled it but I am getting a new error message now that I fixed the problem:
C:\robot>java practice
Testing out the robot process!
Exception in thread "main" java.lang.NullPointerException
at practice.CreateBots(practice.java:30)
at practice.main(practice.java:17)
Here is the code:
import java.lang.Math;
class practice {
protected static double Robots[];
protected static String Program[][];
protected static int ProgramLine[];
protected static int RobotCycle[];
public static void main(String[] args) {
// Title and startup
System.out.println("Testing out the robot process!\n\n");
// Initialize robots
CreateBots(3);
}
private static void CreateBots(double AmountOfBots )
{
double PLine;
int i;
int i2;
for (i = 0; i < AmountOfBots; i++) {
//Give robot a userid (not useful now)
Robots[i] = Math.round(Math.random() * 1000);
//Write random program for robot
for ( i2 = 0; i2 < 10; i2++) {
PLine = Math.random() * 10;
if (PLine > 6) Program[i][i2] = "Dance";
else if (PLine > 2) Program[i][i2] = "Flip";
else Program[i][i2] = "Loop";
}
}
return;
}
}
Thank you very much.
buntine
01-29-2005, 08:58 PM
The following line is causing the error:
Robots[i] = Math.round(Math.random() * 1000);
This is because you are referring to an array that does not exist. Before you can assign a value to an array member, you have to define the array itself.
Define the array like this:
int Robots = new int[amountOfBots]; // This line should be placed in the CreateBots() function, but before the for loop.
Also, you should always use an upper-case character at the start of a class name. eg Practise, not practise.
Regards,
Andrew Buntine.
abudabit
01-29-2005, 09:17 PM
Thank you very much for your reply. Now I am getting a different set of errors when I try to compile it:
C:\robot>javac practice.java
practice.java:19: incompatible types
found : int[]
required: int
int Robots = new int[AmountOfBots];
^
practice.java:20: incompatible types
found : java.lang.String[][]
required: java.lang.String
String Program = new String[AmountOfBots][10];
^
practice.java:21: incompatible types
found : int[]
required: int
int ProgramLine = new int[AmountOfBots];
^
practice.java:22: incompatible types
found : int[]
required: int
int RobotCycle = new int[AmountOfBots];
^
practice.java:29: array required, but int found
Robots[i] = Math.round(Math.random() * 1000);
^
practice.java:34: array required, but java.lang.String found
if (PLine > 6) Program[i][i2] = "Dance";
^
practice.java:35: array required, but java.lang.String found
else if (PLine > 2) Program[i][i2] = "Flip";
^
practice.java:36: array required, but java.lang.String found
else Program[i][i2] = "Loop";
^
8 errors
C:\robot>
Here is the subroutine that is producing the errors:
private static void CreateBots(int AmountOfBots )
{
int Robots = new int[AmountOfBots];
String Program = new String[AmountOfBots][10];
int ProgramLine = new int[AmountOfBots];
int RobotCycle = new int[AmountOfBots];
double PLine;
int i;
int i2;
for (i = 0; i < AmountOfBots; i++) {
//Give robot a userid (not useful now)
Robots[i] = Math.round(Math.random() * 1000);
//Write random program for robot
for ( i2 = 0; i2 < 10; i2++) {
PLine = Math.random() * 10;
if (PLine > 6) Program[i][i2] = "Dance";
else if (PLine > 2) Program[i][i2] = "Flip";
else Program[i][i2] = "Loop";
}
}
return;
}
Also, doesn't creating the variables in the subroutine make them local?
buntine
01-29-2005, 09:24 PM
Ok, we have to tell java that these are arrays. Try this:
int [] Robots = new int[AmountOfBots];
String [][] Program = new String[AmountOfBots][10];
int [] ProgramLine = new int[AmountOfBots];
int [] RobotCycle = new int[AmountOfBots];
Hopefully, that will work now.
And, yes, creating the variables within a routine will make when local private variables.
Regards.
abudabit
01-29-2005, 09:39 PM
Cool, thank you very much. Got it working.
ray326
01-29-2005, 11:51 PM
Your capitalization is still totally screwed up. Classes should be capitalized. Methods (except for constructors) and variables should be lower case. Constants (final statics) should be all caps.
class MyClass
{
private int someNum;
private ArrayList someArrayList;
public static final int MAGIC_NUMBER = 42;
MyClass MyClass()
{
super();
}
int getSomeNum()
{
return someNum;
}
}
Got it?