Jump to content

Java: Can't get value from LinkedList to another class

Go to solution Solved by WanderingFool,

I don't know if these are the exact problems, and MrSuperb talks about an issue that I will repeat, because it is a bit one.

 

So the first issue that I see (well more of an comment and fix).  Don't just use plain LinkLink in Java.  You should be setting the default types.

LinkedList<Brick> brickList = new LinkedList<Brick>(); ///How it should be

This way you will actually be able to get Bricks out of the list instead of casting it later on.

 

Now for the second issue, which seems like you might need to brush up on how variables are handled in Java.

    public Object addBrick(int brick_positionX, int brick_positionY, int brick_lenght, int brick_height, boolean isVisible)    {        System.out.println("Brick.addBrick();");        Brick brick = new Brick();                           //Creating a new brick        this.brick_positionX = brick_positionX;       //Setting the current brick, the the brick variable to the value        this.brick_positionY = brick_positionY;       //See above        this.brick_lenght = brick_lenght;        this.brick_height = brick_height;        this.isVisible = isVisible;        return brick;   //You are now returning the brick variable...but you have not even changed a single thing, so the variables will be all default values (ie 0 for ints and false for booleans)    }

MrSuperB's solution is correct though.  And as Ciccioo said, you really should have revisited the original post...it mentions the biggest fault which is the addBrick, which needs to be fixed first

We are programming Pinball and need your help:

 

Our program doesn't return the values of level_bricklist.

run:Entering the Level();Adding Bricks to Level 0...Brick.addBrick();Brick.addBrick();Brick.addBrick();brick_positionX = 0 brick_positionY = 0 brick_lenght = 0 brick_height = 0 brick_isVisible = truebrick_positionX = 0 brick_positionY = 0 brick_lenght = 0 brick_height = 0 brick_isVisible = true[...]Entering the Level();Adding Bricks to Level 0...Brick.addBrick();Brick.addBrick();Brick.addBrick();brick_positionX = 0 brick_positionY = 0 brick_lenght = 0 brick_height = 0 brick_isVisible = truebrick_positionX = 0 brick_positionY = 0 brick_lenght = 0 brick_height = 0 brick_isVisible = true[...]brick_positionX = 0 brick_positionY = 0 brick_lenght = 0 brick_height = 0 brick_isVisible = trueBUILD SUCCESSFUL (total time: 11 seconds)

Further information: http://pastebin.com/aNhymNVW

 

Thanks in advance :)

Of course, it is completly normal to have a GPU at 90°c all the time <_< 

 

Java, Sysadmin, Network Engineer, Project Management

 

 

Love you all - thanks for helping me! ^_^

 

Link to comment
Share on other sites

Link to post
Share on other sites

    public Object addBrick(int brick_positionX, int brick_positionY, int brick_lenght, int brick_height, boolean isVisible)    {        System.out.println("Brick.addBrick();");        Brick brick = new Brick();        this.brick_positionX = brick_positionX;        this.brick_positionY = brick_positionY;        this.brick_lenght = brick_lenght;        this.brick_height = brick_height;        this.isVisible = isVisible;        return brick;    }

why don't you override the constructer?

By calling  brick.addBrick(200, 15, 25, 25, true) you are chaing the variables of brick and NOT the newly created one.

Mini-Desktop: NCASE M1 Build Log
Mini-Server: M350 Build Log

Link to comment
Share on other sites

Link to post
Share on other sites

you are essentially doing this right now

level_bricklist.add(brickONE.addBrick(200, 15, 25, 25, true));
    public Object addBrick(int brick_positionX, int brick_positionY, int brick_lenght, int brick_height, boolean isVisible)    {        System.out.println("Brick.addBrick();");        Brick brickTWO = new Brick();        brickONE.brick_positionX = brick_positionX;        brickONE.brick_positionY = brick_positionY;        brickONE.brick_lenght = brick_lenght;        brickONE.brick_height = brick_height;        brickONE.isVisible = isVisible;        return brick;    }

brickTWO (which you add to the list) never gets initialized with any values (other than 0)

Mini-Desktop: NCASE M1 Build Log
Mini-Server: M350 Build Log

Link to comment
Share on other sites

Link to post
Share on other sites

instead you (probably) should do this:

level_bricklist.add(new Brick(200, 15, 25, 25, true));
public Brick(int brick_positionX, int brick_positionY, int brick_lenght, int brick_height, boolean isVisible)    {        this.brick_positionX = brick_positionX;        this.brick_positionY = brick_positionY;        this.brick_lenght = brick_lenght;        this.brick_height = brick_height;        this.isVisible = isVisible;    }

Mini-Desktop: NCASE M1 Build Log
Mini-Server: M350 Build Log

Link to comment
Share on other sites

Link to post
Share on other sites

-snip-

-doublesnip-

now... that's the same exact error that was going on weeks (months?) ago when you asked the same question, and the same answer was given

 

i just want to point out that there is no point in asking questions without listening the answer

Link to comment
Share on other sites

Link to post
Share on other sites

I don't know if these are the exact problems, and MrSuperb talks about an issue that I will repeat, because it is a bit one.

 

So the first issue that I see (well more of an comment and fix).  Don't just use plain LinkLink in Java.  You should be setting the default types.

LinkedList<Brick> brickList = new LinkedList<Brick>(); ///How it should be

This way you will actually be able to get Bricks out of the list instead of casting it later on.

 

Now for the second issue, which seems like you might need to brush up on how variables are handled in Java.

    public Object addBrick(int brick_positionX, int brick_positionY, int brick_lenght, int brick_height, boolean isVisible)    {        System.out.println("Brick.addBrick();");        Brick brick = new Brick();                           //Creating a new brick        this.brick_positionX = brick_positionX;       //Setting the current brick, the the brick variable to the value        this.brick_positionY = brick_positionY;       //See above        this.brick_lenght = brick_lenght;        this.brick_height = brick_height;        this.isVisible = isVisible;        return brick;   //You are now returning the brick variable...but you have not even changed a single thing, so the variables will be all default values (ie 0 for ints and false for booleans)    }

MrSuperB's solution is correct though.  And as Ciccioo said, you really should have revisited the original post...it mentions the biggest fault which is the addBrick, which needs to be fixed first

0b10111010 10101101 11110000 00001101

Link to comment
Share on other sites

Link to post
Share on other sites

-anothersnip-

the old post was quite complete, it mentioned all of that too, also the typization of the linkedlist (and a small discussion about it, from which i learnt something)

Link to comment
Share on other sites

Link to post
Share on other sites

Why a linked list?

main(i){for(;i<101;i++)printf("Fizz\n\0Fizzz\bBuzz\n\0%d\n"+(!(i%5)^!!(i%3)*3)*6,i);}

Link to comment
Share on other sites

Link to post
Share on other sites

Why a linked list?

 

Because we have to (as a student).

Of course, it is completly normal to have a GPU at 90°c all the time <_< 

 

Java, Sysadmin, Network Engineer, Project Management

 

 

Love you all - thanks for helping me! ^_^

 

Link to comment
Share on other sites

Link to post
Share on other sites

the old post was quite complete, it mentioned all of that too [...]

 

now... that's the same exact error that was going on weeks (months?) ago when you asked the same question, and the same answer was given

 

i just want to point out that there is no point in asking questions without listening the answer

 

 

I don't know if these are the exact problems, and MrSuperb talks about an issue that I will repeat, because it is a bit one.

 

So the first issue that I see (well more of an comment and fix).  Don't just use plain LinkLink in Java.  You should be setting the default types.

LinkedList<Brick> brickList = new LinkedList<Brick>(); ///How it should be

This way you will actually be able to get Bricks out of the list instead of casting it later on.

 

Now for the second issue, which seems like you might need to brush up on how variables are handled in Java.

    public Object addBrick(int brick_positionX, int brick_positionY, int brick_lenght, int brick_height, boolean isVisible)    {        System.out.println("Brick.addBrick();");        Brick brick = new Brick();                           //Creating a new brick        this.brick_positionX = brick_positionX;       //Setting the current brick, the the brick variable to the value        this.brick_positionY = brick_positionY;       //See above        this.brick_lenght = brick_lenght;        this.brick_height = brick_height;        this.isVisible = isVisible;        return brick;   //You are now returning the brick variable...but you have not even changed a single thing, so the variables will be all default values (ie 0 for ints and false for booleans)    }

MrSuperB's solution is correct though.  And as Ciccioo said, you really should have revisited the original post...it mentions the biggest fault which is the addBrick, which needs to be fixed first

 

 

the old post was quite complete, it mentioned all of that too, also the typization of the linkedlist (and a small discussion about it, from which i learnt something)

 

Oh yes, there it is.  Hawkins you really should have gone back to your original post

 

a small discussion about it, fro

 

Sorry for not looking back into the last thread - However, luckily we have now managed to solve the problem thanks to you guys :)

 

It seems that we didn't understand the first version you tried to explain the problem to us, but this one helped us alot!

 

Thanks :)

Of course, it is completly normal to have a GPU at 90°c all the time <_< 

 

Java, Sysadmin, Network Engineer, Project Management

 

 

Love you all - thanks for helping me! ^_^

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×