我目前正在做一个单一的任务,因为我不是很熟悉编码。如何通过字符串从JTextArea通过drawString没有得到空指针
该赋值是为了让用户创建一个类图。目前我有一个辅助框架,用户在JTextArea中输入类的UML设计,然后通过用户编写并将字符串(drawString)绘制到创建的类上,将其传递给我的GUI。
当我试图从JTextArea获取字符串drawString时,我得到一个nullpointerexception,我不明白这一点,因为肯定inputUML.getText()会是一个字符串,所以应该能够通过?
这是我试图将字符串传递到
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;
}
}
这个类是具有JTextArea中的类,它是另一个类和包,但应检索串类扩展这个类。
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;
}
}
所以我想知道什么,简单地说,就是我怎么能解决这个问题,有没有只是行我会错了吗?或者我在试图达到什么样的逻辑问题?
谢谢:)
请学习java命名约定并坚持到他们 – kleopatra 2012-03-01 13:15:14
对不起,我想我现在已经修复了这个问题。 – DanMc 2012-03-01 13:23:33
修复了什么?即使从我在滚动前可以看到的代码中,属性'instructionlabel'应该是'instructionLabel','CreationPanel'应该是'creationPanel''''Create'应该是'create'。关于类,方法和属性名称,请参考JLS的[Java命名法](http://java.sun.com/docs/books/jls/second_edition/html/names.doc.html#73307)以获取更多信息。 – 2012-03-01 14:07:00