Java in BlueJ does not execute main class - java

I am using the BlueJ IDE. I have a main class entitled ProgramOne, and another class StarTurtle (intended to serve an instance method).
Here is the code of ProgramOne:
public class ProgramOne
{
public static void main (String[ ] args) {
StarTurtle turtle1 = new StarTurtle();
int result = turtle1.StartTurtle(5);
}
}
Here is the code of StarTurtle:
public class StarTurtle
{
private int points;
public int StartTurtle(int x)
{
points = x;
Turtle sue;
sue = new Turtle();
sue.paint (90, 40);
}
}
(The turtle method you see is from two other classes that I have not pasted here for the sake of brevity. These classes are found in the http://www.cs.ccsu.edu/~jones/book.htm manual)
The code only compiles, and there is no option to execute. However, there is no option to execute void main (String[ ] args), which there should be to execute the main class. Does anyone know what is the cause of this? I am assuming that there is a problem in the code itself. The StarTurtle class does execute, but the main class ProgramOne does not, which leads me to believe that the problem lies in the ProgramOne class.
When I mean "option to execute", I am referring to this BlueJ functionality:

When you call the main method from the class ProgramOne, you have created an instance of StarTurtle turtle1. But when you assign value in result variable by calling turtle1.StartTurtle(5) method, nothing is stored in that variable. The problem is that you have issue in this function in your StarTurtle class. You have defined public int StartTurtle(int x) function as return type but actually it is not returning any thing. Therefore, you need to add a return statement on that block of code.
public class StarTurtle {
private int points;
public int StartTurtle(int x){
points = x;
Turtle sue;
sue = new Turtle();
sue.paint (90, 40);
return points;
}
}
However, on the other hand, your class and functions are incorrect though they are working. You classes should be like this.
public class ProgramOne{
public static void main (String[ ] args) {
StarTurtle turtle1 = new StarTurtle();
turtle1.StartTurtle();
}
}
public class StarTurtle {
//Void type of method instead of previous return type
public void StartTurtle(){
Turtle sue;
sue = new Turtle();
sue.paint (900, 550);
}
}

Related

Cant access methods from another class

Im calling DataComparison()
public class SteganographyGUI {
...
DataComparison dataComp;
dataComp = new DataComparison();
}
public int getLSB(){
String x = fileChooser1.getSelectedFile().getAbsolutePath();
x = x.substring(x.length() - 10, x.length() - 9);
return Integer.parseInt(x);
}
when some criteria are met. My problem is that, when I try to access getLSB by using gui.getLSB()
public class DataComparison {
public static SteganographyGUI gui;
...
public DataComparison(){
lsb = gui.getLSB();
}
public static void main(String[] args) {
gui = new SteganographyGUI();
gui.setVisible(true);
}
Error appears - Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
How can I fix this?
You are trying to call getLSB() in your DataComparison class, but you don't give it the reference of your SteganographyGUI class. So change the following line:
DataComparison dataComp;
dataComp = new DataComparison();
to:
DataComparison dataComp;
dataComp = new DataComparison(this);
And change the constructor as well:
public DataComparison(SteganographyGUI guiRef){
gui = guiRef;
}
You are doing cyclic calls in your code. Try another class just for the main and call its methods in order:
class 1: `public class SteganographyGUI {...}`
class 2: `public class DataComparison {...}
class 3:
`public class XXXX {
public static void main(String[] args) {
gui = new SteganographyGUI();
gui.setVisible(true);
}
}`
I have helped :)
When you try to initiate static "gui" variable
gui = new SteganographyGUI();
you execute SteganographyGUI class constructor which (probably) calls DataComparison class constructor
public DataComparison(){
lsb = gui.getLSB();
}
which uses static "gui" variable which is not set yet. This is the reason you got "java.lang.NullPointerException".
Yes, I know "the exception message is misleading" :)

Making a static duplicate of non-static integer

For my programming class in first year engineering I have to make a D-game in Java, with only very little knowledge of Java.
In one class I am generating a random integer via
public int rbug = (int)(Math.random() * 18);
every so many ticks. I have to use this integer in another class (in the requirements for an if-loop), and apparently it needs to be static. But when I change the variable to public int static, the value doesn't change any more.
Is there an easy way to solve this problem?
Edit: part of code added:
public int rbug = (int)(Math.random() * 18);
which is used in
public void render(Graphics g){
g.drawImage(bugs.get(rbug), (int)x, (int)y, null);
And in another class:
if(Physics.Collision(this, game.eb, i, BadBug.rbug)){
}
As error for BadBug.rbug I get the message
Cannot make a static reference to a non-static field
Using static to make things easier to access is not a very good ideal for design. You would want to make variables have a "getter" to access them from another class' instance, and possibly even a "setter". An example of this:
public class Test {
String sample = 1337;
public Test(int value) {
this.sample = value;
}
public Test(){}
public int getSample() {
return this.sample;
}
public void setSample(int setter) {
this.sample = setter;
}
}
An example of how these are used:
Test example = new Test();
System.out.println(example.getSample()); // Prints: 1337
example = new Test(-1);
System.out.println(example.getSample()); // Prints: -1
example.setSample(12345);
System.out.println(example.getSample()); // Prints: 12345
Now you might be thinking "How do I get a string from the class that made the instance variable within the class?". That's simple as well, when you construct a class, you can pass a value of the class instance itself to the constructor of the class:
public class Project {
private TestTwo example;
public void onEnable() {
this.example = new TestTwo(this);
this.example.printFromProject();
}
public int getSample() {
return 1337;
}
}
public class TestTwo {
private final Project project;
public TestTwo(Project project) {
this.project = project;
}
public void printFromProject() {
System.out.println(this.project.getSample());
}
}
This allows you to keep single instances of classes by passing around your main class instance.
To answer the question about the "static accessor", that can also be done like this:
public class Test {
public static int someGlobal = /* default value */;
}
Which allows setting and getting values through Test.someGlobal. Note however that I would still say that this is a horrible practice.
Do you want to get a new number every time that you want BadBug.rbug? Then convert it from a variable to a method.

Pass an object of another class to a singleton class

I am using this singleton class in Java and in one method, I need an object of a class which gets instantiated in Main. I am not knowing how to pass that object to this method because this code is written in the constructor of the singleton class as I need it to be executed as soon as the program starts.
Should I take out the code from the constructor and make it a standalone method which I call from Main (though I wouldn't prefer this) or is there another way?
Any ideas?
Code:
Main:
public static void main(String[] args) {
X x; // This is the object I need to pass to the singleton class
}
Singleton class:
public SomeSingletonClass {
private Queue<Y> someQueue; // Y is another class I have in my project
private SomeSingletonClass(){
someQueue.add(new Y(<some data>, <some data>, <here I need an object of X as the constructor needs it>);
}
}
I haven't added the entire code. Just a fragment where I am stuck.
You have two main options.
The first will produce howls of derision - and rightly so because it is a dark tunnel of hell.
public class X {
}
public class Y {
public Y(String s, X x) {
}
}
public class Main {
public static X x = new X();
}
public class SomeSingletonClass {
private Queue<Y> someQueue = new LinkedList<>();;
private SomeSingletonClass() {
someQueue.add(new Y("Hello", Main.x));
}
}
Here we make the X created by Main a public static so it is now, essentially, global state in parallel with your singleton.
Most readers will understand how nasty this is but it is the simplest solution and therefore often the one taken.
The second option is lazy construction.
public class BetterSingletonClass {
private BetterSingletonClass me = null;
private Queue<Y> someQueue = new LinkedList<>();
private BetterSingletonClass(X x) {
someQueue.add(new Y("Hello", x));
}
public BetterSingletonClass getInstance (X x) {
if ( me == null ) {
me = new BetterSingletonClass(x);
}
return me;
}
}
Note that I have made no effort to make this a real singleton, n'or is this thread-safe. You can search for thread safe singleton elsewhere for plenty of examples.

Why do I need a class here?

class Myclass{
int x;
Myclass(int i){
x = i;
}
}
class UseMyclass { //Why do I need another class?
public static void main (String args[]){
Myclass y = new Myclass(10);
System.out.println(y.x);
}
}
Why can't I run main() out of Myclass?
From the book it says I would be running the UseMyclass so I guess that would be my file name. Why couldn't I just use the Myclass though as the file name and run main() in there?
I'm new to programming so I'm just trying to figure stuff out.
You don't actually need another class. If you just put the main method into the class, it will work. For example, this code will work just fine:
class Myclass{
int x;
Myclass(int i){
x = i;
}
public static void main (String args[]){
Myclass y = new Myclass(10);
System.out.println(y.x);
}
}
However, separating the main class is a good idea when you are dealing with large programs with many classes. Then, you can sneak unit tests into main methods in other classes.

java.lang.NullPointerException when filling an array of objects

There's something I'm missing here. with this code I get a java.lang.NullPointerException:
public static void main(String[] args) {
Board board = new Board();
board.Initialise();
}
public class Board {
private Obj[][] tableau;
public void Board() {
tableau = new Obj[8][8];
}
public void Fill_Board() {
tableau[0][0]= new Obj('B');
}
}
But with this other code I get no error. What I am doing wrong, and how to initialize properly this array of object?
public class Board {
private Obj[][] tableau = new Obj[8][8];
public void Board() {
}
public void Fill_Board() {
tableau[0][0]= new Obj('B');
}
}
Board() ends up being a member function and not a constructor, and therefore never gets called. The problem is the void keyword, which needs to be removed:
public Board() { /* removed the `void' */
tableau = new Obj[8][8];
}
I get no error when I run this code (changing Obj to an actual class), perhaps you can provide a more concrete example with a main method of what you're trying to do?
If you're looking at making a constructor then it needs to be the same name as your class, i.e. Tab and have no return type.
So you would need to add:
public Tab() {
// initialization code here
}
You're constructor will run whenever you create a new instance of that class. You want to use it to initialize all of your variables.
Tab t = new Tab(); // Constructor runs
Edit:
You're main method uses class Board but you have given us a class called Tab. I can assume that you really want class Board so you should change all Tab to Board in the above example, if that's what you're looking for.

Categories

Resources