Can't create a Object - java

I have an app with multiple classes:
MenuActivity, MenuThread, MenuView, MenuBot, MenuBall.
In the class "MenuView" I declare all the ib objects I need:
this.ball = new MenuBall(this, bot1);
this.bot1 = new MenuBot1(this, ball);
this.thread = new MenuThread(this,bot1,ball);
As you can see i didn't create yet the object bot1 but i already use it as a parameter in the object ball, which gives me the error.
Thank you for trying to help me !

You have to change (or add other) constructors of MenuBall and MenuBot1.
Thus, for example:
public class MenuBall {
private MenuBot1 menuBot1;
(...)
// this constructor doesn't need a MenuBot1 object.
public MenuBall(MenuView menuView) {
(...)
}
// setter for the menuBot1
public void setMenuBot1(MenuBot1 menuBot1) {
this.menuBot1 = menuBot1;
}
(...)
}
public class MenuBot1 {
private MenuBall menuBall;
(...)
// this constructor doesn't need a MenuBall object.
public MenuBot1(MenuView menuView) {
(...)
}
// setter for the menuBall
public void setMenuBall(MenuBall menuBall) {
this.menuBall = menuBall;
}
(...)
}
Then in MenuView class:
ball = new MenuBall(this);
bot1 = new MenuBot1(this);
ball.setMenuBot1(bot1);
bot1.setMenuBall(ball);
thread = new MenuThread(this, bot1, ball);
(...)

Related

values of protected arrays on superclass get changed unexpectedly

I'm coding a chess engine using bitboards and I wanna make an extensible API for the bitboards initializer, that would allow me to add more variants like chess960 in future.
So i came up with the following abstract superclass, which gives an uniform interface for all kind of initializers: it allocates the arrays used to store the bitboards and then calls the abstract method init() that has to be implemented by any subclass, inside there the bitboards should be created and assigned to the respective array
public abstract class BitboardInitializer {
private static final int NUMBER_OF_PLAYERS = 2;
protected long[] pawnsPositions, knightsPositions, bishopsPositions,
rooksPositions, queenPositions, kingPositions;
protected BitboardInitializer() {
pawnsPositions = knightsPositions = bishopsPositions =
rooksPositions = queenPositions = kingPositions = new long[NUMBER_OF_PLAYERS];
init();
}
protected abstract void init();
public long getPawnsPositionsAs(int side) {
return pawnsPositions[side];
}
public long getKnightsPositionsAs(int side) {
return knightsPositions[side];
}
public long getBishopsPositionsAs(int side) {
return bishopsPositions[side];
}
public long getRooksPositionsAs(int side) {
return rooksPositions[side];
}
public long getQueenPositionsAs(int side) {
return queenPositions[side];
}
public long getKingPositionsAs(int side) {
return kingPositions[side];
}
}
An implementation to initialize standard chess bitboards, it simply assign the hard-coded bitboards values because standard chess starts always in one way. Side.White and Side.Black are two static final fields used as array indexes to avoid inconsistence. white = 0, black = 1:
public final class StandardChessInitializer extends BitboardInitializer {
public StandardChessInitializer() {
super();
}
protected void init() {
pawnsPositions[Side.WHITE] =
0b00000000_00000000_00000000_00000000_00000000_00000000_11111111_00000000L;
pawnsPositions[Side.BLACK] =
0b00000000_11111111_00000000_00000000_00000000_00000000_00000000_00000000L;
knightsPositions[Side.WHITE] =
0b00000000_00000000_00000000_00000000_00000000_00000000_00000000_01000010L;
knightsPositions[Side.BLACK] =
0b01000010_00000000_00000000_00000000_00000000_00000000_00000000_00000000L;
bishopsPositions[Side.WHITE] =
0b00000000_00000000_00000000_00000000_00000000_00000000_00000000_00100100L;
bishopsPositions[Side.BLACK] =
0b00100100_00000000_00000000_00000000_00000000_00000000_00000000_00000000L;
rooksPositions[Side.WHITE] =
0b00000000_00000000_00000000_00000000_00000000_00000000_00000000_10000001L;
rooksPositions[Side.BLACK] =
0b10000001_00000000_00000000_00000000_00000000_00000000_00000000_00000000L;
queenPositions[Side.WHITE] =
0b00000000_00000000_00000000_00000000_00000000_00000000_00000000_00010000L;
queenPositions[Side.BLACK] =
0b00010000_00000000_00000000_00000000_00000000_00000000_00000000_00000000L;
kingPositions[Side.WHITE] =
0b00000000_00000000_00000000_00000000_00000000_00000000_00000000_00001000L;
kingPositions[Side.BLACK] =
0b00001000_00000000_00000000_00000000_00000000_00000000_00000000_00000000L;
}
}
The problem is that by calling any superclass' getter method I get binary 1000 for white (index 0) and 100000000000000000000000000000000000000000000000000000000000 for black (index 1) instead of the assigned values
An explanation of this strange behaviour would be highly appreciated, thanks in advance.
The problem is in the BitboardInitializer constructor, where you are initializing all of your array references to point to the same array:
protected BitboardInitializer() {
pawnsPositions = knightsPositions = bishopsPositions =
rooksPositions = queenPositions = kingPositions = new long[NUMBER_OF_PLAYERS];
init();
}
Should be:
protected BitboardInitializer() {
pawnsPositions = new long[NUMBER_OF_PLAYERS];
knightsPositions = new long[NUMBER_OF_PLAYERS];
bishopsPositions = new long[NUMBER_OF_PLAYERS];
rooksPositions = new long[NUMBER_OF_PLAYERS];
queenPositions = new long[NUMBER_OF_PLAYERS];
kingPositions = new long[NUMBER_OF_PLAYERS];
init();
}

How does java lambdas external references work?

I'm wondering how does lambdas external references work. Let me explain:
Suppose i have this supplier implementation and this model class :
public class TestSupplierImpl implements Supplier<Boolean> {
public Predicate<Integer> predicate;
public TestSupplierModel model;
public TestSupplierImpl() {
this.predicate = i -> model.something.equals(i);
}
#Override
public Boolean get() {
return predicate.test(3);
}
}
class TestSupplierModel {
public Integer something;
public TestSupplierModel(Integer something) {
this.something = something;
}
}
Then i execute the following code:
TestSupplierImpl test = new TestSupplierImpl(); // line 1
test.model = new TestSupplierModel(3); // line 2
Boolean resultado = test.get(); // line 3
Line 1: creating a new instance of TestSupplierImpl. This new instance's predicate has a null reference of model. This makes sense because at the moment of creation of the predicate, model reference is null.
Line 2: assign to variable model a new instance of TestSupplierModel.
Line 3: test.predicate now has model reference with the new assigned value. Why is this ?
I don't understand why ,when I changed model reference, the predicate updates its model reference to the new one. How is that ?
Thanks in advance !
Does it make sense if you rewrote your TestSupplierImpl() constructor as follows?
public Predicate<Integer> predicate;
public TestSupplierModel model;
public TestSupplierImpl() {
// same effect as this.predicate = i -> model.something.equals(i);
this.predicate = new Predicate<Integer>() {
public boolean test(Integer i) {
return model.something.equals(i);
}
};
}
#Override
public Boolean get() {
return predicate.test(3);
}
So here is the order of things.
// the constructor is run and the test method defined BUT NOT executed.
TestSupplierImpl test = new TestSupplierImpl(); // line 1
// Now you define model
test.model = new TestSupplierModel(3); // line 2
// Then you execute the predictate via get()
Boolean resultado = test.get(); // line 3
model and something aren't required until you issue the get() method. By that time they are already defined.

JavaFx observale entity without change the entity itself

I have a class of entity which have a lot of field and I would like to create observable entity to this class so I can use this to Binding.
The entity's code can't be change. how can I do it?
Let's assume that my code look like that:
class Car {
private Wheel wheel;
private Engine engine;
private Door door;
// and a lot of field.
}
and I want to create ObservableCar that become invalidate each time any of the field change.
The car code doesn't change. And the class of the field too.
How can I do it?
Thanks in advance.
Implement your Car class using JavaFX properties:
public class Car {
private final ObjectProperty<Wheel> wheel = new SimpleObjectProperty<>();
public ObjectProperty<Wheel> wheelProperty() {
return wheel ;
}
public final Wheel getWheel() {
return wheelProperty().get();
}
public final void setWhee(Wheel wheel) {
wheelProperty.set(wheel);
}
// other properties...
}
Now you can create a ObjectBinding<Car> that invalidates when any of the properties invalidate:
public class CarBinding extends ObjectBinding<Car> {
private final Car car ;
public CarBinding(Car car) {
this.car = car ;
bind(car.wheelProperty(), car.engineProperty(), ...);
}
#Override
public Car computeValue() {
return car ;
}
}
If it's more convenient, you can build the binding directly into your car class:
public class Car {
// properties as before...
private final ObjectBinding<Car> carBinding = new ObjectBinding<Car>() {
{
bind(wheelProperty(), engineProperty(), ...);
}
#Override
public Car computeValue() {
return Car.this ;
}
}
public ObjectBinding<Car> asBinding() {
return carBinding ;
}
}

Getter and setter for List

class MipRequest{
private List<String> MipIDs=null;
public List<String> getMipIDs() {
return MipIDs;
}
public void setMipIDs(List<String> mipIDs) {
MipIDs = mipIDs;
}
}
How can i call the get function?
You could call it by using object instance like:
MipRequest mipRequest = new MipRequest();
//set list...
List<String> mipIds = mipRequest.getMipIDs();
//further business logic

How to navigate in a circle

the title might be not very descriptive but i couldn't think of a better one.
The problem is as follows:
I have one screen (ScreenOne) with a link to another screen (ScreenTwo).
On the ScreenTwo is a link back to ScreenOne.
I implemented this via custom RichTextFields and a custom ChangeListener.
Now the problem is that i keep getting a StackOverflowError!
Is there any way to navigate back and forth in that way?
regards matt
public class MyApp extends UiApplication
{
public static void main(String[] args)
{
MyApp theApp = new MyApp();
theApp.enterEventDispatcher();
}
public MyApp()
{
ScreenOne so = ScreenProvider.getInstance().getScreenOne();
so.initialize();
ScreenProvider.getInstance().getScreenTwo().initialize();
pushScreen(so);
}
}
public class ScreenOne extends MainScreen {
MyTextField link;
public ScreenOne() {
link = new MyTextField("FirstScreen");
add(link);
}
public void initialize(){
link.setChangeListener((FieldChangeListener) new MyFieldChangeListener(ScreenProvider.getInstance().getScreenTwo()));
}
}
public class ScreenTwo extends MainScreen {
MyTextField link;
public ScreenTwo() {
link = new MyTextField("SecondScreen");
add(link);
}
public void initialize(){
link.setChangeListener((FieldChangeListener) new MyFieldChangeListener(ScreenProvider.getInstance().getScreenOne()));
}
}
public class MyFieldChangeListener implements FieldChangeListener {
private Screen nextScreen;
public MyFieldChangeListener(Screen nextScreen) {
this.nextScreen = nextScreen;
}
public void fieldChanged(Field field, int context) {
UiApplication.getUiApplication().pushScreen(nextScreen);
}
}
public class MyTextField extends RichTextField {
public MyTextField() {
super();
}
public MyTextField(String text) {
super(text);
}
protected boolean touchEvent(TouchEvent message) {
if (TouchEvent.CLICK == message.getEvent()) {
FieldChangeListener listener = getChangeListener();
if (null != listener)
listener.fieldChanged(this, 1);
}
return super.touchEvent(message);
}
}
public class ScreenProvider {
private static ScreenProvider instance = null;
private ScreenProvider(){}
public static ScreenProvider getInstance() {
if (instance == null) {
instance = new ScreenProvider();
}
return instance;
}
private ScreenOne screenOne = new ScreenOne();
private ScreenTwo screenTwo = new ScreenTwo();
public ScreenOne getScreenOne() {
return screenOne;
}
public ScreenTwo getScreenTwo() {
return screenTwo;
}
}
The constructor of ScreenOne creates a ScreenTwo instance, and the constructor of ScreenTwo creates a ScreenOne instance. You have an infinite loop here.
Regarding revision 5 of this question:
new ScreenProvider() -> new ScreenOne() -> ScreenProvider.getInstance() -> new ScreenProvider() -> ...
still infinite. Again, the problem is that you're trying to setup a cycle via object constructors. You need to create the objects first, then assign the next and previous.
Regarding revision 4 of this question:
getScreenOne() -> new ScreenOne() -> getScreenTwo() -> new ScreenTwo() -> getScreenOne() -> newScreenOne() -> ...
you still have an infinite loop, because the constructors are trying to store an instance of each other. You need to construct the objects first, then add the cyclic references.
In your ScreenProvider you don't need to make screen1/screen2 static -- they're members of the singleton instance.
Outside of that the other problem I see in this current version is that you're going to be pushing a screen onto the stack -- that's already on the stack. Try popping the prior screen first.
That overflow error is likely the result of an infinite loop caused by constantly jumping from ScreenOne and ScreenTwo. Could you describe what you actually would want to accomplish and/or show a snippet of code?

Categories

Resources