• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Cannot find main class?

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is just a testing program, that's why you see the "//code goes here"'s. I'm just trying to get it to create and print a set for me
Here's my code:


And here's the error it gives me:
java.lang.ExceptionInInitializerError
Caused by: java.lang.NullPointerException
at settest.MyArraySet.<init>(Main.java:50)
at settest.Main.<clinit>(Main.java:11)
Could not find the main class: settest.Main. Program will exit.
Exception in thread "main"
Exception in thread "main" Java Result: 1

The 50th line(that it shows in the error) is the 43rd line(in my code pasted here), and the 11th line is the 4th line. It says it couldn't find Main, does anybody know why that is?

 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Derek,
You have a lot of static in this. If you trace through the order things happen, you'll see where the null pointer comes from.

1) Call main
2) JVM realizes it needs to instantiate everything static
3) JVM starts with mySet and calls constructor MyArraySet (note nothing else static has been instantiated yet)
4) String [] arrStrEle=Main.arrStr; - Main.arrStr is still null so arrStrEle has a reference to a null.
5) arrStrEle.length - null.length gives you the null pointer
 
Derek Szpik
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes, I do see the null pointer error, but underneath the error lines you see how it can't find settest.Main? is that because the null pointer error stops the program before it gets to main(not the class main, the other one).
 
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you follow the steps that Jeanne specified above you will see why it is a bad idea not to encapsulate (make private) the member variables and it is a bad idea to access them directly like that from other classes.

As far as “code goes here” are concerned it is usually a good idea not to change the code for posting. Obviously the code you posted does not compile but since you are reporting a NullPointerException it means that you were able to compile the code, I am just saying this because it makes it easy to find the error if you post the actual code.
 
Moojid Hamid
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you fix the first error and try to run it again?
 
Derek Szpik
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I didn't change the code for posting, like I said it's just a test program and the only reason those methods are there is so that MyArraySet properly implements MySet without errors. In my test program when running it actually has //code goes here, because I wasn't testing those methods, I was just filling them in. I needed to ahve the methods there because even empty they allow MyArraySet to properly implement MySet. And yes, the null pointer exception was the real problem, thank you Jeanne. I also changed some things that I spotted I didn't need, a lot of the static things that I put into MyArraySet can be taken out, I put them there initially because I thought I'd need them, and then didn't because I changed a lot since then, so yeah, those are gone too.
 
Moojid Hamid
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i don't see how



will compile without an actual return statement in it.
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Derek Szpik wrote:yes, I do see the null pointer error, but underneath the error lines you see how it can't find settest.Main? is that because the null pointer error stops the program before it gets to main(not the class main, the other one).


For the future, this is a stack trace. The real/useful error is on the top. The rest is the path of calls that got you there.
 
Derek Szpik
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you again jeanne.

@moojid
I think you misunderstand the point of that method being there, I'm not testing to see if isIn will compile, or any of the other methods. What I'm doing here is just trying to make sure that mySet will be created correctly. This is why none of the methods have any code in them. After I initialize mySet, I print mySet. This shows me which spot in memory my set was created in, if nothing is printed then I know that mySet wasn't created.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic