• Post Reply Bookmark Topic Watch Topic
  • New Topic

Declaring Method in main class  RSS feed

 
Jerry Girgich
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So this is a silly question but I can't for the life of me remember how to properly declare a void method in the main class.



This is the method, would I declare like this or is it something else? :

 
fred rosenberger
lowercase baba
Bartender
Posts: 12464
43
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jerry Girgich wrote: would I declare like this or is it something else? :



What happened when you tried it?  That's the fun part about programming...you can just try something and see what happens.
 
Henry Wong
author
Sheriff
Posts: 23028
120
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jerry Girgich wrote:
This is the method, would I declare like this or is it something else? :


Not sure what you are asking. The code that you shown (in the first code tag) is the method declaration... or are you asking how to call the method that was declared?

Henry
 
Jerry Girgich
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry I meant call the method, and when I do

getKey(args);

I get an Exception in thread "main" java.lanf.NUllPointerException
 
Dave Tolls
Ranch Hand
Posts: 2560
27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That line won't give a NullPointerException, so you'll need to look at the stack trace and see which line it is that's causing the issue.
 
Jerry Girgich
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:That line won't give a NullPointerException, so you'll need to look at the stack trace and see which line it is that's causing the issue.




There are two lines

1.) This one is in the getKey method



2.)
 
fred rosenberger
lowercase baba
Bartender
Posts: 12464
43
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
keyPair is null, so you get the NPE when you call keyPair.getPrivate().


I would check your getKeyPair method and make sure it's actually returning a KeyPair.
 
Dave Tolls
Ranch Hand
Posts: 2560
27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I say, the stack trace will tell you which one is causing the problem.
Now, since (2) can't cause the problem, then I can safely guess it's (1), but you do need to learn how to read a stack trace with confidence...

On that assumption, keyPair is null.

To confirm that you can add

to the line before.
 
Liutauras Vilda
Marshal
Posts: 3961
214
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Once you are convinced keyPair is null, go to the method declaration getKeyPair("C:/Users/*****/keystore.jks") and see in which circumstances it returns that.
 
Jerry Girgich
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a way to get my private key without creating a certificate for the public key? I have already made a certificate for the public key and the only way I could figure out how to get the private key is this:



This creates a certificate for the public key but I have already done this using keytool a while ago. Also this key pair does return null
 
Jerry Girgich
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can it be possible that since the public key is expired it would give me a null keypair?

Please let me know what the problem is and how I would go about fixing it.
 
Henry Wong
author
Sheriff
Posts: 23028
120
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jerry Girgich wrote:Can it be possible that since the public key is expired it would give me a null keypair?


Speculating is probably *not* the most efficient option here. Basically, it the keypair is likely null, because the code at line 26 is not running. And line 26 is not running, very likely due to the condition at line 21 failing.

To fix it, you need to confirm it (ie. debug it)... What is the key being returned? Is it an instanceof PrivateKey? etc.  I recommend adding a few print statements, to confirm exactly what is happening first.

Henry
 
Liutauras Vilda
Marshal
Posts: 3961
214
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jerry Girgich wrote:

Your key isn't an instance of PrivateKey probably.

Looking to an API docs, I see they use some other routine to have it as private key
Java 8 API wrote:// get my private key
KeyStore.PrivateKeyEntry pkEntry = (KeyStore.PrivateKeyEntry)
ks.getEntry("privateKeyAlias", protParam);
PrivateKey myPrivateKey = pkEntry.getPrivateKey();

I nowhere see that you'd do something similar to that. Maybe that's an issue. I have never used that class, so don't know in particular.
 
Jerry Girgich
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Speculating is probably *not* the most efficient option here. Basically, it the keypair is likely null, because the code at line 26 is not running. And line 26 is not running, very likely due to the condition at line 21 failing.
To fix it, you need to confirm it (ie. debug it)... What is the key being returned? Is it an instanceof PrivateKey? etc.  I recommend adding a few print statements, to confirm exactly what is happening first.

Henry


So I added a print statement right underneath line 21 and nothing is printing so I think line 21 is definitely the issue. What would I do to fix this?
 
Jerry Girgich
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:

Java 8 API wrote:// get my private key
KeyStore.PrivateKeyEntry pkEntry = (KeyStore.PrivateKeyEntry)
ks.getEntry("privateKeyAlias", protParam);
PrivateKey myPrivateKey = pkEntry.getPrivateKey();



Could I just use this to get the private key instead of the two methods? I've been trying to look online for other ways to get private key and I haven't really found one that makes sense to me.
 
Henry Wong
author
Sheriff
Posts: 23028
120
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jerry Girgich wrote:
Could I just use this to get the private key instead of the two methods? I've been trying to look online for other ways to get private key and I haven't really found one that makes sense to me.


Using code that doesn't "makes sense" is probably not a good idea. Granted, if it works, and continues working, then great -- you saved a bit a time. However, more often than not, you will run into issues, and you can't debug something that you don't understand.

Henry
 
Henry Wong
author
Sheriff
Posts: 23028
120
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jerry Girgich wrote:
So I added a print statement right underneath line 21 and nothing is printing so I think line 21 is definitely the issue. What would I do to fix this?


I asked quite a few questions, in my post which you quoted, which you seemed to not answered. Beside the condition failing, you still don't have much idea of what is going on.

Henry
 
Jerry Girgich
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you think is the best way to fix my original code? I am not sure what else to put in the of statement nor do I know how else to fix it
 
Liutauras Vilda
Marshal
Posts: 3961
214
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To my understanding some of the possible answers can be found here, by reading and trying to understand what this class is doing and how.
 
Jerry Girgich
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:To my understanding some of the possible answers can be found here, by reading and trying to understand what this class is doing and how.



I have been reading this and for this the password is char[], my password is a String so would it still work?
 
Jerry Girgich
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using the code from that link, the password type is the only error I am getting. What can I change so that I can use String for the password?>
 
Liutauras Vilda
Marshal
Posts: 3961
214
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have no idea.

1. Do you know what instanceof operator is doing?
2. Do you know in which circumstances if statement's condition evaluates to true and in which to false?

3. What can you conclude from that? What you think might be a problem?

In my understanding, your concentration is needed at around lines 4-6.
 
Liutauras Vilda
Marshal
Posts: 3961
214
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
More important, you seem to be guessing, same as I.

Do you have any material you were learning from and by any chance you missed them? Probably worthwhile going back and re-reading class notes or tutorial or book once again.
Solving problem in guessing way is very inefficient. You need to narrow down the problem and try to understand each and every piece of code in deep details, only then you can solve the problem in some sensible manner.

Even tho if you get things working without understanding why - it is little to no beneficial after all.
 
Stephan van Hulst
Saloon Keeper
Posts: 7207
118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jerry Girgich wrote:Using the code from that link, the password type is the only error I am getting. What can I change so that I can use String for the password?>

Nothing. NEVER use String for passwords. There's a reason the method accepts a character array. When you're done with a password, zero it out.
 
Jerry Girgich
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I think I understand this a lot more than what I had before. But the variable that is supposed hold the private key is returning null.

when I print it out is returning null and then giving me  nullPointerException error at the last line
 
Liutauras Vilda
Marshal
Posts: 3961
214
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jerry Girgich wrote:But the variable that is supposed hold the private key is returning null.

Documentation I gave earlier says:
Java 8 API wrote:Returns:
the keystore Entry for the specified alias, or null if there is no such entry
Throws:
NullPointerException - if alias is null

Does it ring any bell?
 
Liutauras Vilda
Marshal
Posts: 3961
214
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jerry Girgich wrote:try (FileInputStream fis = new FileInputStream("C:/Users/*****/keystore.jks"))

What a weird path.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!