Why is this code giving an "Unreachable Statement" error? - java

This is my code and im getting an unreachable statement error on it but i do not know why.
public boolean Boardload(String[] args) throws Exception
{
Robot robot = new Robot();
Color color3 = new Color(114, 46, 33);
Color color4 = new Color(180, 0, 0);
{
Rectangle rectangle = new Rectangle(0, 0, 1365, 770);
{
while(false)
{
BufferedImage image = robot.createScreenCapture(rectangle);
search: for(int x = 0; x < rectangle.getWidth(); x++)
{
for(int y = 0; y < rectangle.getHeight(); y++)
{
if(image.getRGB(x, y) == color3.getRGB())
{
return true;
}
}
}
}
}
return false;
}
}
the exact error is:
java:68: unreachable statement
{
^
Help would be nice, this code is supposed to loop until the pixel is found.

I think the problem is that your loop is
while(false) {
This loop never executes, because false != true. Consequently, the Java compiler is telling you that nothing in the body of the loop will ever execute, and hence it's unreachable.
Try changing your loop to
while (true) {
(the idiomatic "loop forever") and see if that fixes things.
Hope this helps!

while(false) is always false and the loop body is never executed: unreachable. Change to while (true).

The statement while(false) will never execute anything within that loop, thus it's all un-reachable.

Sorry, but that is some smelly code. I'm not sure what the braces/blocks are doing after declaring your Color local vars, and after declaring your Rectangle var. The main problem for unreachability is while(false), which means it will never execute the associated block.

Related

How to stop += function in java libgdx

Y keeps on going altough I set it to 0 in Y=0;
So the sprite should go from middle to 750px and then after I click on it it should go up to 650...after i click it on 650 px it should go down to 750 (and so on)
But the code is not important,what I want to know is how to stop adding +5px on Y with some function like Y.stop(); or Y.delete(); or Y.destroy();
Y +=5;
Sorry for so little code here
public int Y,YspriteDown,Yposition;
public boolean AppON = true;
w = Gdx.graphics.getWidth();
h = Gdx.graphics.getHeight();
YspriteDown=h/2+50;
YspriteUp=h/2-50;
batch.begin();
if(AppON= true)
{
Yposition = YspriteDown + Y ;
Y +=5;
if(Yposition == h-50)
{
Y =0;
batch.draw(Assets.sprite1, w / 2 - 16, Yposition);
}
batch.end();
The problem is that the Clicked variable is never set to false, so your outer loop still continues, adding 5 to y until it equals 100 again, then y is set to 0, and the loop continues.
The simple fix is to change the value of the Clicked variable to false inside of your if statement.
if (Clicked)
{
Y +=5;
batch.draw(Assets.sprite1,w/2-16,Y);
if (Yposition == 100)
{
Y = 0;
batch.draw(Assets.sprite1, w / 2 - 16,h-150);
Clicked = false;
}
}
EDIT:
With your new code, you shouldn't set Yspritedown to 0, or else the loop continues like before, it needs to end at h-50 and stay there, not revert to its original position
EDIT:
With your new new code, there needs to be an inner loop that checks if the sprite is still moving, there isn't anyway to "destroy" the variable without causing a NullPointerException, so do something like this
while(appIsRunning){
if(clicked){
boolean doneMoving = false;
y_position += 5
if(Y_Position >= h-50){
y_position = h-50;
batch.draw(arg, arg, arg);
doneMoving = true;
}
}
}

Program freeze after input?

After the user inputs their choices, the program seems to offer the same choices again, followed by it completely freezing. It's only set on there to give them the option once (I believe) and then follow the selections below. Instead it seems to revert back to the top, and then just freeze for some odd reason. Tried figuring it out for a while, but no avail.
public void init() //Initialize method {
setSize(1000, 800); //Set size
Container c = getContentPane();
c.setBackground(Color.GREEN); //Set background
}
public void paint(Graphics g) {
super.paint(g); //Start paint method
g.setFont(new Font("Veranda", Font.PLAIN, 20));
g.setColor(Color.BLACK);
g.drawString("Hello", 250, 25); //top display message
String number = JOptionPane.showInputDialog("Would you like a custom loop count or an infinite? 1. Custom 2. Infinite"); //test choice
n = Integer.parseInt(number);
while (n>0 || n<2)
if (n==1) {
String number2 = JOptionPane.showInputDialog("How many times would you like to loop?");
integer = Integer.parseInt(number2);
while (integer>0)
while (x < integer) {
g.drawString("hi", 200, y);
x+=1;
y = y+40; //test
}//end while
}//end if
else if (n==2) {
while (true); //im aware this is an endless loop. Can't find anything more stable for an endless one, so its just there for now..
}//end if
else;
g.drawString("Please pick a valid choice", 200, 200);
}//end paint
}//end
This is what I understood
while (integer>0) {
while (x < integer)
{
g.drawString("hi", 200, y);
x+=1;
y = y+40; //test
}//end while
integer --; // <-- This is missing
}
break; // <-- Also missing
The inner while ends when x = integer
but integer is always > 0
you have to do integer -- at the end of the outer loop
So it can go to 0 at some point
Please use proper indentation... it helps
I edited the code... see where I put the 'integer --'
try converting your while(n>0 || n<2) to if(n>0 && n<2)
Try not using while instead of if, it creates less confusion
Add brackets {} for all your code blocks, easier to read, hence easier to debug, maybe you'll figure it out by just doing that
Also is the ; after the last else intended?

Java null pointer error

Hello and thank you for reading this. I am coding in java/LWJGL. My problem is that I keep getting a null pointer error with some code if I don't include this one thing. Basically there are 4 classes
A block class.
A blockGrid class
A blocktype class
And a boot class.
The boot class creates a display and in the game loop it runs the draw method that is inside the blockgrid class. To set where a block goes i would use the renderat(x,y) method inside the blockgrid method. The block class just creates a quad at a certain x,y.
Sorry if I'm not explaining good. Here is my code: This is where the error happens just read my comments to see where the error is.
// BlockGrid.java
package minecraft2d;
import java.io.File;
public class BlockGrid {
private Block[][] blocks = new Block[100][100];
public BlockGrid() {
for (int x = 0; x < 25 - 1; x++) {
for (int y = 0; y < 16 - 1; y++) {
blocks[x][y] = new Block(BlockType.AIR, -100, -100); //This is where my error happens! If I don't include this line i get a null pointer. Anything will help. I am really stuck and don't know whats happening
}
}
}
public void setAt(int x, int y, BlockType b) {
blocks[x][y] = new Block(b, x * 32, y * 32);
}
public void draw() {
for (int x = 0; x < 25 - 1; x++) {
for (int y = 0; y < 16 - 1; y++) {
blocks[x][y].draw();
}
}
}
}
The reason you get a NullPointerException when you don't have that line is that blocks[x][y] will be null for all x,y when draw() is called. draw() assumes you have valid Block objects because it's calling Block#draw.

Return outside method error

boolean openingboard;
{
Robot robot = new Robot();
Color color3 = new Color(108, 25, 85);
Rectangle rectangle = new Rectangle(0, 0, 1365, 770);
while(true)
{
BufferedImage image = robot.createScreenCapture(rectangle);
search: for(int x = 0; x < rectangle.getWidth(); x++)
{
for(int y = 0; y < rectangle.getHeight(); y++)
{
if(image.getRGB(x, y) == color3.getRGB())
{
System.out.println("About to finish and return true");
return true;
}
System.out.println("About to finish and return false");
}
}
}
}
the error is:
java:71: return outside method
return true
^
i don't know what this is happening please help!
From your comment response above, I am going to make the educated guess that you believe that
boolean openingboard;
{
return true;
}
defines a Java method called openingboard. This isn't the case. Java follows the C paradigm of requiring you to specify your parameters in parentheses, regardless of whether you have any parameters or not. So, the method
boolean openingboard() {
return true;
}
is a valid Java method (assuming it is inside some class), as is a version of openingboard with much more code between the curly braces.
That said, I'm going to pass along a few friendly pointers on Java style:
Java (and indeed most higher-level language) programmers tend to frown on "forever" loops such as while (true), since those loops make it much harder to determine when the loop actually stops.
There is no need for the label search in the code, and labels are even more discouraged than forever loops are.
So, I would recommend rewriting your code to look something like
private boolean openingboard() {
Robot robot = new Robot();
Color color3 = new Color(108, 25, 85);
Rectangle rect = new Rectangle(0, 0, 1365, 770);
BufferedImage image = robot.createScreenCapture(rect);
for(int x = 0; x < rectangle.getWidth(); x++) {
for(int y = 0; y < rectangle.getHeight(); y++) {
if(image.getRGB(x, y) == color3.getRGB())
return true;
}
}
return false;
}
assuming of course that you prefer a debugger to trace prints.
Proper methods look like: boolean openingboard ( )
not like boolean openingboard;
The parenthesis are not optional.
The way you have it: openingboard is a field. There is a init block with a Robot and a color and some for loops nested inside of each other. Inside one of the for loops is a return which is not allowed in an init block.

if/then with pixel finding and looping, all of which is above me at the moment

I want to take a screenshot, and if the pixel is the correct value RGB then take another screenshot and find next pixel or else repeat.
this is the code to get the pixel and it works like a charm!
{
BufferedImage image = robot.createScreenCapture(rectangle);
search: for(int x = 0; x < rectangle.getWidth(); x++)
{
for(int y = 0; y < rectangle.getHeight(); y++)
{
if(image.getRGB(x, y) == color3.getRGB())
{
break search;
}
}
}
}
what i want to know i guess is how would i go about asking it to repeat this segment of code until the pixel equals the true color. the color i am looking for is:
Color color3 = new Color(114, 46, 33);
Ok context, i am building a program that goes through steps, one opens the given puzzle, i have that down because i can use simple pixel data, then it needs to center the mouse on the center pixel. The problem is i cant just use a second get pixel image because it takes a while for the game to open the relevant jpanel so i need my program to wait until it can find a pixel indicating the game is open before it starts to look for the pixel to center the mouse.
You can probably separate the screenshot code into a method and call it until you get the desired result:
public boolean checkColor(Color inputColor) {
BufferedImage image = robot.createScreenCapture(rectangle);
for(int x = 0; x < rectangle.getWidth(); x++) {
for (int y = 0; y < rectangle.getHeight(); y++) {
if (image.getRGB(x, y) == inputColor.getRGB()) {
return true;
}
}
}
return false;
}
This method will return true if it can find the given inputColor in the screenshot. You might then use it in a loop as follows:
Color newColor = ...;
while (!checkColor(newColor)) {
new Color = new Color(114, 46, 33);
// Or change color in here for every iteration
}
This loop will terminate if it can't match the screenshot to newColor.

Categories

Resources