Easy - JTextArea won't get the text? and more stuff is wrong?
First of all , I'm new here :)
My name is Kasper and I'm new at java (want to learn it)
I'm from holland so my english isn't perfect.
so I've created a very basic program which can calculate the Easter date for each year.
this is the clas with the problem ( it's easy for you but for me it is hard (new at java))
Code :
package Pasen;
import java.awt.event.*;
import javax.swing.*;
public class PaasPaneel extends JPanel implements ActionListener
{
private JTextField Jaartalveld;
private JButton Bereken;
private JTextArea uitkomstveld;
int paasMaand;
int paasDag;
public String einddatum;
public PaasPaneel()
{
JTextField Jaartalveld= new JTextField(4); // just doing basic stuff
JButton Bereken = new JButton("Bepaal paasdatum");
Bereken.addActionListener(this); // adds actionlistener to this JButton
JTextArea uitkomstveld = new JTextArea(12, 35);
add(new JLabel("Jaar"));
add(Jaartalveld);
add(Bereken);
add(uitkomstveld);
}
/*
* This should calculate all the stuff
*/
public void JaartalTester(int beginJaartal)
{
System.out.println ("hoi");
if (beginJaartal < 1900 || beginJaartal > 2099 ) // checks if the input is right
{
JOptionPane.showMessageDialog( // show's this if the input is wrong
null,
"het jaartal moet tussen 1900 en 2099 zijn",
"Onjuist jaartal",
JOptionPane.ERROR_MESSAGE);
}
else
{
int A = beginJaartal % 19; // more calculation
System.out.println(A); // debugging , same as below
int B = beginJaartal % 4;
System.out.println("dit is 2" + B);
int C = beginJaartal % 7;
int D = (19 * A + 24) % 30;
int E = (5 + 2 * B + 4 * C + 6 * D) % 7;
int F = D + E - 9;
System.out.println("dit is " +F); // more debugging
if (F > 0)
{
paasMaand = 4;
System.out.println("waarde paasMaand = " + paasMaand);
if (F == 26)
{
paasDag = 19;
}
else
{
if (F == 25 && D == 28)
{
paasDag = 18;
}
else
{
paasDag = F;
System.out.println("hij komt hier");
}
}
}
else
{
paasMaand = 3;
paasDag = F + 31;
}
}
if (! (paasMaand <= 0 || paasDag <= 0))
{
einddatum =("in " + beginJaartal + "valt pasen op de dag " + paasDag + "van maand " + paasMaand); // gives the String "einddatum" a value
System.out.println(einddatum);
uitkomstveld.setText(einddatum); // this should set the value of "uitkomstveld" to "einddatum" but it doesn't work yet.
}
}
public void actionPerformed (ActionEvent e)
{
int beginJaartal = 2007; //random default value
// Integer.parseInt(Jaartalveld.getText()); this should create a String which contains the text of "jaartalveld" but it doesn't work right know.
System.out.println("het beginjaartal is " + beginJaartal); //some debugging
JaartalTester(beginJaartal); // calls the method JaartalTester
}
}
when I run this program the console is like this:
Code :
het beginjaartal is 2007
hoi
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Pasen.PaasPaneel.JaartalTester(PaasPaneel.java:95)
at Pasen.PaasPaneel.actionPerformed(PaasPaneel.java:111)
12
dit is 23
dit is 8
waarde paasMaand = 4
hij komt hier
in 2007valt pasen op de dag 8van maand 4
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
sorry I'm such a noob >.< but I really don't know what to do..
Hope you can help me :)!
thanks!
Re: Easy - JTextArea won't get the text? and more stuff is wrong?
Quote:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Pasen.PaasPaneel.JaartalTester(PaasPaneel.java:95)
There is a variable with a null value at line 95 when it is executed. Look at line 95, find the variable with the null value and then backtrack in the code to see why that variable does not have a valid value.
Re: Easy - JTextArea won't get the text? and more stuff is wrong?
huh?
line 94 :
Code :
String einddatum = ("in " + beginJaartal + "valt pasen op de dag " + paasDag + "van maand " + paasMaand); // gives the String "einddatum" a value
line 95 + 96:
Code :
System.out.println(einddatum);
uitkomstveld.setText(einddatum); // this should set the value of "uitkomstveld" to "einddatum" but it doesn't work yet.
in line 94 I gave "einddatum" a value and in line 95 + 96 I said print einddatum and set the text of "uitkomstveld" to "einddatum".
what variable does not have a value at this point? I don't understand this?
EDIT: "einddatum" has a variable, if I print it out :
[in 2007 valt pasen op de dag 8 van maand 4]
and what's wrong with line 110? :
Code :
JaartalTester(beginJaartal); // calls the method JaartalTester
I don't understand this and this is very confusing for me :S.
Re: Easy - JTextArea won't get the text? and more stuff is wrong?
Code :
uitkomstveld.setText(einddatum);
If this is line 95,
What value does uitkomstveld have?
Re: Easy - JTextArea won't get the text? and more stuff is wrong?
it doesn't have a value at that point .. I think.. it's a JTextArea. Isn't it normal that it has no value at that point?
Code :
uitkomstveld.setText(einddatum);
So here I try to set the value of uitkomstveld to "einddatum" but it won't work.
Re: Easy - JTextArea won't get the text? and more stuff is wrong?
What value does the variable: uitkomstveld have? Print out the variable and see if it has a value.
You expect it to have the address of a text area. Does it? I think it has a null value and is what is giving the exception. Print it and see.
Re: Easy - JTextArea won't get the text? and more stuff is wrong?
well, the value is null :P
how do I fix that? what value do I have to give to a JTextArea ? no idea ..
Re: Easy - JTextArea won't get the text? and more stuff is wrong?
Quote:
what value do I have to give to a JTextArea
You use the new statement with a constructor to give object reference variables their value.
Re: Easy - JTextArea won't get the text? and more stuff is wrong?
thanks :D I'm so dumb thank you so much <3