Win a copy of Machine Learning for Business: Using Amazon SageMaker and JupyterE this week in the Jython/Python forum
or Object Design Style Guide in the Object-Oriented programming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

main method doesn't work

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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



im really confused, i cannot see why its broken
 
Ranch Hand
Posts: 118
5
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
john kaine
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
john kaine
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
at javafx.stage.Window.<init>(Window.java:1209)
at javafx.stage.Stage.<init>(Stage.java:239)
at javafx.stage.Stage.<init>(Stage.java:227)
at BoardController.<init>(BoardController.java:20)
at Main.main(Main.java:6)
Caused by: java.lang.IllegalStateException: This operation is permitted on the event thread only; currentThread = main
at com.sun.glass.ui.Application.checkEventThread(Application.java:443)
at com.sun.glass.ui.Screen.setEventHandler(Screen.java:285)
at com.sun.javafx.tk.quantum.QuantumToolkit.setScreenConfigurationListener(QuantumToolkit.java:674)
at javafx.stage.Screen.<clinit>(Screen.java:79)
... 5 more

if i run it in intellij
 
Marshal
Posts: 6643
177
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had a look at your project.  It's not set up like a regular JavaFX project.  The changes needed are are fairly major.

First, the gradle build file:
Next, Main.java:
Next, BoradView.java:
Last, BoardController.java:
I think that's everything.  
 
Knute Snortum
Marshal
Posts: 6643
177
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I forked your GitHub project and added my changes.  It's here:

https://github.com/ksnortum/Connect4

Also, I updated the README.md to describe how to launch the project.
 
john kaine
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 have a couple questions,

whats the reason behind making the boardview extend from borderpane?

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?
 
Knute Snortum
Marshal
Posts: 6643
177
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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:

   --module-path /path/to/javafx-sdk-11.0.2/lib --add-modules javafx.controls
 
john kaine
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


i added that to the boardview start method and then

to launch it, that was the only way i thought could work


and thanks, ill add the gradle things into my project as well
 
Yeah, but does being a ninja come with a dental plan? And what about this tiny ad?
Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps
https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!