• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What's wrong with this code

 
Arun C. Giridharan
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


What's wrong with this code .....

Error:

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Cannot make a static reference to the non-static method newInstance() from the type Class

at SystemProperties.main(SystemProperties.java:17)
 
Rob Spoor
Sheriff
Pie
Posts: 20545
56
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are running this code from an IDE, aren't you? Check out which errors it is giving you. My guess is that cl is uninitialized.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Simply put: the code above wouldn't give the error shown, as you're not calling newInstance() statically. That error could come about if you literally called something like

Class.newInstance();

but you've changed the code since then. Still, this code will give a NullPointerException, as "cl" isn't initialized.

What are you trying to do, exactly?
 
Arun C. Giridharan
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i'm trying to create SystemProperties Object using newInstance() method....i'm not using IDE !! not yet
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arun Giridharan wrote:i'm trying to create SystemProperties Object using newInstance() method....i'm not using IDE !! not yet


Well, yes -- yes you are. The "unresolved compilation problem" message comes from Eclipse.
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Class c1;


cl is only a reference variable.......and if you donot creae its instance then it will give a runtime error.which is nullpointerexceptin
 
Rob Spoor
Sheriff
Pie
Posts: 20545
56
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It won't even get that far. Because it's a local variable it needs to be initialized before it can be used. Initializing it to null would cause the NPE.
 
Arun C. Giridharan
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This doesn't work ...
 
Campbell Ritchie
Sheriff
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Too complicated a question for "beginning Java". Moving thread.

Try
 
Dieter Quickfend
Bartender
Posts: 543
4
Java Netbeans IDE Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Class objects are constructed automatically by the Java Virtual Machine as classes are loaded and by calls to the defineClass method in the class loader."

What you're trying to do, is create a class out of nothing and cast it to your own class. I think you have to define the class first. That would be my guess.
 
Hari haran Ravi
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arun, what you are trying to do, is that you are calling a non static method named newInstance in the class named "Class". Well, actually, in the class "Class" which is predefined, there are no static methods named "newInstance()".

To make your code work, just try using "SystemProperties.getClass().newInstance()"

I hope it works.

Regards
 
Campbell Ritchie
Sheriff
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I earlier wrote:Try
Actually it's not Class but Class<T>, so it should be Class<SystemProperties>.

And did you notice my mistake writing "class" instead of "Class"? I have only just noticed it. Sorry.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic