• Post Reply Bookmark Topic Watch Topic
  • New Topic

Unable to find the name of the current package  RSS feed

 
Abhay Bhatt
Ranch Hand
Posts: 64
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have two class files: Hello and Test.

I have the below code:-



In the above code for class 'Hello', you can see that no reference to the class 'Test' is made. When I am compiling, I am getting no error. But when I am running it, I am getting: Exception in thread "main" java.lang.StackOverflowError and then multiple lines of: at Test.<init>(Test.java:5)

I have below questions:-

1). When I am replacing Hello.class.getPackage().getName(); with Hello.getPackage().getName(); ie removing 'class' keyword, then I am getting error on compiling. Why is that so? Because I've read at multiple places that you can get package name by using ClassName.getPackage().getName()

2). Why I am getting the above described error on running the above code? And how can I fix it?

3). Why the class 'Test' name is mentioned in the error, when no reference/relation is there to class Test in the above code?
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when I am running it,


What is the contents of the command-line when you "run" the program?


  I am getting error on compiling

Please copy the full text of the error message and paste it here. It has important info about the error.
 
Abhay Bhatt
Ranch Hand
Posts: 64
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:
when I am running it,


What is the contents of the command-line when you "run" the program?


I have already stated that above in my original problem statement.

Norm Radder wrote:
  I am getting error on compiling

Please copy the full text of the error message and paste it here. It has important info about the error.


I am getting below error:-

Hello.java:11: error: cannot find symbol
                String s = Hello.getPackage().getName();
                                ^
  symbol:   method getPackage()
  location: class Hello
1 error



 
Abhay Bhatt
Ranch Hand
Posts: 64
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abhay Bhatt wrote:
I am getting below error:-

Hello.java:11: error: cannot find symbol
                String s = Hello.getPackage().getName();
                                ^
  symbol:   method getPackage()
  location: class Hello
1 error





The up arrow in the output was pointed towards the 'dot' between "Hello" and "getPackage()", and not towards "Hello" as shown above. Probably an error in pasting from cmd.
 
Pete Letkeman
Ranch Foreman
Posts: 898
26
Android Chrome IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I took your code for Hello.java it compiled fine.
However when I attempted to run the code I got the following:

Null Pointer Exception usually happens when a object reference has not been initialized or it is the object reference is pointed to null.

If you rename Hello to something else, say MyHello and update the your code does it compile?
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

is the syntax for calling the static method getPackage() in the class Hello.  The compiler gives an error message because it can not find that method.

I have already stated that above in my original problem statement.

Sorry I do not see it shown in your post.  I was looking for something like this:   A command-line that executes the java command with Hello
C:\MyFolder\>java Hello
showing the java command with one argument: Hello
 
Abhay Bhatt
Ranch Hand
Posts: 64
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pete Letkeman wrote:When I took your code for Hello.java it compiled fine.


If you rename Hello to something else, say MyHello and update the your code does it compile?


Just in case you missed it, the compiler error occurs when I remove the 'class' keyword.
 
Abhay Bhatt
Ranch Hand
Posts: 64
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:  Hello.getPackage()
is the syntax for calling the static method in the class Hello.  The compiler gives an error message because it can not find that method.


Can you please elaborate on above? So, how one is suppose to use this method to know the package name, ordinarily?

Norm Radder wrote:
Sorry I do not see it shown in your post.  I was looking for something like this:   A command-line that executes the java command with Hello
C:\MyFolder\>java Hello
showing the java command with one argument: Hello


Sorry, I thought you were asking for the output. Here you go:-

D:\Manu\Study>java Hello
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how one is suppose to use this method

You need to look at the API doc for the class that method is in to see how to use it.
On the API doc home page http://docs.oracle.com/javase/8/docs/api/index.html, click Index at the top and then click on the letter that the method starts with: G
all the methods starting with that letter will be displayed.  Search that list of methods to find the class(es) the method is defined in.  click on the method name to go to its API doc.
 
Abhay Bhatt
Ranch Hand
Posts: 64
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:
You need to look at the API doc for the class that method is in to see how to use it.


So, I looked it and found that getPackage() belongs to java.lang package. So, I imported this package by: import java.lang.*;

But I am still getting the same error message. Any idea as to why the compiler is unable to find the method 'getPackage()'?
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why the compiler is unable to find the method 'getPackage()'?

methods belong to classes.  You need a reference to an instance of the class to be able to call its methods:

found that getPackage() belongs to java.lang package

I see 3 classes that the method getPackage is in.  Two of the methods take a String argument.

Note: The class name you refer to is Package not package. Class names begin with uppercase letters
Where does your code have a reference to an instance of the Package class so that it can call its getPackage method?
 
Fred Kleinschmidt
Bartender
Posts: 571
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your original post is showing the entire source listing for your Hello class, then the call to will return null since you have not specified a package for your code. Then the call getName() will attempt to dereference that null, thus the exception.
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The OP did not report a NPE.  His post gave this as the error:

Exception in thread "main" java.lang.StackOverflowError and then multiple lines of: at Test.<init>(Test.java:5)


Another poster: Pete Letkeman got the NPE.

There might be some confusion about what program was being executed in the first post.
 
Tim Cooke
Marshal
Posts: 4037
239
Clojure IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The NPE was observed by Pete, which is the same behaviour I see too when I ran it. However, the OP originally reported a StackOverflowError. I suspect there's parts of the whole application that aren't being shown here.

I fully expect that Norm is carefully steering the OP to their own "ah ha!" moment when they figure it out for themselves.
 
Abhay Bhatt
Ranch Hand
Posts: 64
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:
  You need a reference to an instance of the class to be able to call its methods:


Where does your code have a reference to an instance of the Package class so that it can call its getPackage method?


I am so much confused regarding how to use in-built methods and packages. May be I didn't gave this a thought recently! I can understand that you may need the instance of the class to call its method. But, if I create an instance of the Package class, I will get the package name of that class, whereas I want to know the package name of my current class ie Hello. I may be getting you wrong, but if what you say is true, then no one will be able to use these in-built methods for their own purpose.
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to know the package name of my current class ie Hello

The original code you posted does that:

You need to study about classes and methods.  See the tutorial:
http://docs.oracle.com/javase/tutorial/java/javaOO/classes.html
http://docs.oracle.com/javase/tutorial/java/javaOO/returnvalue.html
 
Abhay Bhatt
Ranch Hand
Posts: 64
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:
You need to study about classes and methods.  See the tutorial:

I am really sorry. Its my bad! I misunderstood some code statements in another article. My question is originally wrong.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!