Reputation: 564
I am trying to run a java class fwd_TLit which extends the class layout_ETP which is my layout for a project. It is build using gridBag layout. Now I am facing NullPointerException when I try to run the fwd_TLit.Plz someone helps me what will be the error and get no output. The code for the layout_ETP :-
public class layout_ETP {
static Font f=new Font("Papyrus",Font.BOLD ,50);
static Font f1=new Font("Papyrus",Font.BOLD ,30);
public static JLabel l,l1,l2;
public static JButton clearBtn,convertBtn,obj;
public static JTextArea ta,ta1;
public static JPanel footer;
public static void main(String[] args) {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame();
frame.setSize(600, 600);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setFont(f);
frame.setTitle("Natural Language Processor");
frame.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
// Setting the label for the nlp
l = new JLabel("Natural Language Processor");
l.setFont(f);
c.weightx = 0.0;
c.gridx = 0;
c.gridy = 0;
c.ipady = 20;
c.fill = GridBagConstraints.NONE;
c.anchor = GridBagConstraints.FIRST_LINE_START;
frame.add(l, c);
l1 = new JLabel("English");
l1.setFont(f1);
c.gridy = 1;
c.weighty = 0.0;
frame.add(l1, c);
}
The code for the fwd_TLit :-
public class fwd_TLit extends layout_ETP {
public void transliteration() {
ta.setText("Hello");
String str = ta.getText().toString();
ta1.setText(str);
}
public static void main(String[] args) {
try{
fwd_TLit obj = new fwd_TLit();
obj.transliteration();
} catch(Exception e) {
System.out.println(e);
}
}
}
Sorry for that guys but I am trying to put the smallest code from that class..!! I have all the necessary things such as fonts,there declaration etc...but facing this prob in the fwd_TLit class. I think the problem must be there..!!
Upvotes: 0
Views: 93
Reputation: 2923
... EDIT: Just read the comment from Teeg. If I run layout_ETP.java, it works for me (after setting f and f1).
fwd_TLit is throwing java.lang.NullPointerException. It contains two variables "ta" and "ta1", where are they created?
So what do you want to do?
Upvotes: 0
Reputation: 11072
This is partially in answer to your comment.
As I pointed out in the comments, you have two main methods. Though you have not posted the stacktrace, the only place I can see that would cause a NullPointerException
is in the fwd_TLit
class's main method, which is what I assume is actually running.
In fwd_TLit
, you're attempting to use fields from its parent class which have not yet been instantiated. Here is what I am referring to:
public void transliteration() {
ta.setText("Hello"); // RIGHT HERE
String str = ta.getText().toString();
ta1.setText(str); // AND TECHNICALLY HERE AGAIN
}
While those variables are indeed instantiated in the parent class's main method, that main method is not automatically called. My suggestion would be to create some kind of static initializer method in which you create all those objects (which I believe would be most or all of the code in the layout_ETP
class's main method). You would call that static initializer method BEFORE you call transliteration
, or any other method that would require the use of those fields.
Side Note:
Since you're instantiating a new fwt_TLit
object, those variables (such as ta
and ta1
, etc), don't need to be static.
As Boris the Spider and Scorpion pointed out, please please please (I can't stress this enough), please follow the Java naming convention styles (which can be found in the link Scorpion provided). Looking at your code, I have absolutely no clue, at a glance, what fwt_TLit
, or ta
, ta1
(...etc) are or do, nor am I able to determine what is a class (which should begin with an uppercase letter), and what is a variable (which begin with lowercase letter). Readability, in software development, is paramount for code maintainability (and not angering your coworkers).
Upvotes: 1