Can't get java code to assign a user's entered data to a object field in Joptionpane

I am working on some code where I have to create a program and class. The class named “Pet” has the fields, “Name”, “Animal”, and “Age”, And the program I have to create takes entered data and spits it back out to the user. I’ve created the class for it just fine, but when it comes to the actual program, I’m having trouble assigning the object’s name to what the user enters.

I’ve tried changing the input type.

//Template GUI Diaglog.

import javax.swing.JOptionPane;

public class Project_3
   public static void main(String[] args)

      String input;

       Pet obj = new Pet(); 

      input = JOptionPane.showMessageDialog (null, "Enter the pet's name ");


      JOptionPane.showMessageDialog (null, "The Pet's name is " + obj.getName());



Leave a Reply

1 Comment threads
0 Thread replies
Most reacted comment
Hottest comment thread
1 Comment authors
Jason Recent comment authors
newest oldest most voted
Notify of

JOptionPane.showMessageDialog(null, "Enter the pet's name ") returns null, instead try using JOptionPane.showInputDialog("Enter the pet's name ") which should return a string of the user’s input.

You can find more information about JOptionPane here.