ive finished a mini connect4 game that i tried. If you know what bluej is it can let you create objects of classes and whatnot without using a main method. When i use bluej and create the classes individually and pass them to the controller it works, but if i use the main method to do a similar thing i get an error.
calling this constructor gives error on 2nd last line
it says this gives a nullpointerexception for a reason
Sorry, but even BlueJ does use a main() at least somewhere - as this how vanilla Java launch actually starts a Java program.
Next: A NullPointerException is something that happens during runtime - so your code actually runs until it hits the NPE - your observation your main() "wouldn't work" is not correct.
There're a few other issues with the few lines posted (I can imagine how the rest looks like *scary face*), but I guess it would be a start to do either of these things:
1) Post a link to your repo so someone can have a look at its entirety - as posting walls of code (although serve the same purpose) not like to be seen here (can't understand why - but I didn't came up with it).
2) Replace (or add) every of your catch blocks to contain a Throwable.printStackTrace() and post the complete exception along with matching code.
3) Criple it down to what's called a "short compileable example": That's all your code stripped but only leaving the code that causes the issue (don't use the term "error" unless you actually got one - as most what you get are either compiletime errors (wich you don't) or exceptions (wich you do). Getting an Error during runtime is some very serious problem and should be addressed on its own.)
From what you said and the lines you posted it's likely that boardView.getEnter() return null hence the following statement fails. To verify split it up into two statements and check what boardView.getEnter() really returns - it's most likely NULL.
How did I figured that out: On the line you mentioned you call some method on an object you just passed in - that can't be null as a constructor either fails with an Exception or returns a reference to a fully initialized and valid object. The other part is that setOnAction with a reference to a method - that can't be null either as it's checked by the compiler causing a compiler error. The access of the setOnAction method to what ever boardView.getEnter() returns is all that's left can be easy checked.
Side-note: You have terrible naming for your methods: BoardView.getText().getText() - so you have a field named "text" in your class BoardView wich isn't a String but some other type also have a method called getText to get an actual String - same goes for your "enter" and most likely otber fields and their accessors. An overhaul would be wise and advised.
posted 1 month ago
repo here it is, im not a very experienced programmer so its not gonna be very good
please if possible give me any criticism, i would like to know how i can improve for the future if you have time
about the naming, yeah i realised it was weird but i guess i was just a little lazy lol text was a textfield and enter is a button
posted 1 month ago
ok i added something to the boardcontroller constructor and now the main method will run if i call it from bluej but gives
Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: java.lang.IllegalStateException: This operation is permitted on the event thread only; currentThread = main
... 5 more
john kaine wrote:wow, thanks a lot. i managed to get mine to launch but in a completely different way, but im new to javafx so i see now how it supposed to actually work .
I'd be interested in seeing how you did that.
whats the reason behind making the boardview extend from borderpane?
You don't have to, and I usually don't extend a pane, but I was trying to leave as much of the original code intact. If you're going to create a new object to put into Scene, it has to be some kind of Pane. The other way to do it would be to have a method in BoardView return some kind of Pane, but perhaps getting a new BoardPane is cleaner in this situation.
with the gradle file containing the plugins for javafx, could it not work with the normal imports for javafx at the top of the classes?
No, for reasons I'm not all that clear on. It has to do with JavaFX not being part of Java any more and the Module system that I think was introduced in Java 10. You have to tell whatever's launching JavaFX what modules you need. For instance, if you launch from the command line, you have to include something like this in the vm parameters: