В настоящее время я выполняю уникальное задание как более новое, поэтому я не очень разбираюсь в кодировании.
Назначение - позволить пользователю создать диаграмму классов.В настоящее время у меня есть вторичный фрейм, в котором пользователь вводит UML-дизайн класса в JTextArea, а затем он должен быть передан в мой графический интерфейс, беря то, что написал пользователь, и рисуя строку (drawString) в созданном классе.
В настоящее время я получаю nullpointerexception при попытке получить строку из JTextArea в drawString, и я не понимаю этого, потому что обязательно 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, он находится в другом классе и пакете, но класс, который должен извлекатьstring Расширяет этот класс.
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;
}
}
Итак, я просто хочу узнать, как я могу это исправить, есть ли какая-то линия, в которой я ошибаюсь?или у меня огромная логическая проблема в том, чего я пытаюсь достичь?
Спасибо :))