I am currently doing a uni assignment as a fresher so I’m not very experienced with coding.
The assignment is to allow the user to create a class diagram. Currently I have a secondary frame that the user inputs a UML design of the class in a JTextArea and then this should be passed through to my GUI by taking what the user wrote and drawing the string (drawString) onto the created class.
Currently I am getting a nullpointerexception when trying to get the String from the JTextArea to drawString and I don’t understand this because surely inputUML.getText() would be a String so should be able to be passed through?
This is the class that I am trying to pass the string into
package main;
import java.awt.*;
import javax.swing.*;
import classdesign.ClassCreation;
public class GroupCreateClass {
private double x;
private double y;
private double r;
private String message;
private String classdesign;
public GroupCreateClass(double x, double y, double r) {
this.x = x;
this.y = y;
this.r = r;
}
public void draw(Graphics g) {
makeRectangles(g);
deleteBox(g);
userdesignofclass(g);
}
public void makeRectangles(Graphics g) {
g.drawRect((int)Math.round(x-r),(int)Math.round(y-r),
(int)Math.round(325.0*r),(int)Math.round(350.0*r));
g.drawRect((int)Math.round(x-r),(int)Math.round(y-r),
(int)Math.round(325.0*r),(int)Math.round(19.5*r));
}
public void deleteBox(Graphics g) {
g.fillRect((int)Math.round(x-r),(int)Math.round(y-r),
(int)Math.round(19.5*r),(int)Math.round(19.5*r));
}
public void userdesignofclass(Graphics g){
message = new String("Class");
g.drawString(message,(int)Math.round(x+140),(int)Math.round(y+15));
classdesign = (String.valueOf(inputUML.getText())); // This is the code that is giving me a nullpointerexception. I don't understand why, as surely inputUML.getText() should be a String...?
g.drawString(classdesign,(int)Math.round(x+200), (int)Math.round(y+15));//
}
public double distanceTo(double x, double y) {
return (Math.abs(this.x-x) + Math.abs(this.y-y));
}
public void update(double x, double y) {
this.x = x;
this.y = y;
}
}
This is the class that has the JTextArea, it is in another class and package, but the class that should be retrieving the string Extends this class.
package classdesign;
import java.awt.*;
import javax.swing.*;
public class ClassCreation extends JFrame {
private JFrame frame;
private JLabel instructionlabel;
protected JTextArea inputUML; //This is the JTextArea containing the text that I am trying to pass through.
private JButton create;
public void initGUI(){
frame = new JFrame();
frame.setSize(325, 350);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Class Design - Fill in Class UML");
JPanel CreationPanel = new JPanel();
CreationPanel.setLayout(new BorderLayout());
inputUML = new JTextArea("Write UML here");
inputUML.setLineWrap(true);
inputUML.setWrapStyleWord(true);
CreationPanel.add(new JScrollPane(inputUML),BorderLayout.CENTER);
CreationPanel.add(inputUML,BorderLayout.CENTER);
Create = new JButton("Create Class");
CreationPanel.add(Create,BorderLayout.SOUTH);
//Create.addActionListener(this);
frame.add(CreationPanel);
}
public Frame getFrame() {
return frame;
}
}
So what I want to know, simply, is how I can fix that, is there just a line I’m going wrong? or do I have a massive logic problem in what I’m trying to achieve?
Thanks 🙂
The simple answer to your question is that it’s quite possible you haven’t instantiated
inputUML[ostensibly by callinginitGUI()] before callingstrings()[which is a terrible name for a method].But your problems go far deeper than this. This use of inheritance is completely wrong. Favor composition over inheritance. The use of Magic Numbers is not recommended. The variable names are poor (Java is case sensitive, btw. uml is not the same as UML)…do yourself a favor and start browsing Head First Java and Head First Object Oriented Analysis and Design. Also consider Test Driven Development by Example I know you think you don’t have the time, but it’s much harder to unlearn bad habits than take a little extra time at the beginning to build good ones.