This is a HW problem. I keep getting the following error on screen related to my if(i==3) statement...
"Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
at ui.panels.ChoicePanel$1.itemStateChanged(ChoicePanel.java:31)"
...Can someone point me in the right direction? Here is my code. Thanks for your time.
package ui.panels;
import java.awt.Choice;
import java.awt.Panel;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import model.Model;
import interfaces.Resettable;
public class ChoicePanel extends Panel implements Resettable{
public int i = 0;
/**
*
*/
private static final long serialVersionUID = 1L;
Model model;
Choice selection;
public ChoicePanel(Model mdl) {
selection = new Choice();
for (String msg : Model.selections) {
selection.add(msg);
}
selection.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if(i==3) {//drop down clicked three times)
System.out.println("PING");
}else{
model.setMessage(selection.getSelectedItem());
//this line is what sends a value to shape that is drawn on screen
//NOT line 36 of GUIDemo.java
//
model.setCurrentShapeType(selection.getSelectedItem());
model.repaint();
++i;
}
}
});
this.add(selection);
}
public void resetComponents() {
//this resets the drop down list selection array to the first choice on the list
selection.select(0);
//this sets selected item in the selection array set in the above line
model.setMessage(selection.getSelectedItem());
//model.repaint();
}
}
I believe you are not initializing model, which would give a NullPointerException when it was first dereferenced.
You did not initialize model. If i is not 3 the else-block will get executed, which contains model.setMessage(...) - but model does not yet "exist".
Related
So I'm making a little sample application that displays train times.
Right now the JTable in which the trains are displayed isn't dynamic.
What I'd like, is for the time to be checked every 30 secs or minute and the table would eliminate a train that "should have arrived".
I'm stuck however at the constantly checking time part. I've created a TimeChecker class to run in the background but it's not much use as when I put a infinite loop or thread into my JPanel class, the user interface doesn't show.
Here's my TimeChecker:
package controller;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class TimeChecker extends Thread {
private String timeStamp;
public String getTimeStamp() {
return timeStamp;
}
public void run() {
while(true) {
try {
Thread.sleep(2000);
String dateStamp = new SimpleDateFormat(
"yyyyMMdd_HHmmss").format(
Calendar.getInstance().getTime());
timeStamp = dateStamp.substring(9,13);
System.out.println(timeStamp);
} catch(InterruptedException e) {
e.printStackTrace();
}
}
}
}
and here's my Panel class:
package view;
import java.awt.GridLayout;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import controller.*;
public class Panel extends JPanel {
/* JPanel containing the JTabbedPane stuff */
JTabbedPane jtp = new JTabbedPane();
TimeChecker tc = new TimeChecker();
public Panel() {
super(new GridLayout(1,0));
Table t = new Table("Haymarket");
jtp.add(t);
jtp.setTitleAt(0, "Haymarket");
add(jtp);
tc.setDaemon(true);
tc.start();
}
public void addTrain(String d, String c, String dep, String a) {
/*
* Receives 4 parameters to control
* the new entry required for a train
*/
Table t = (Table) jtp.getComponentAt(jtp.getSelectedIndex());
t.addTrain(d,c,dep,a);
}
public void addStation(String s) {
/*
* Adds a station, after prompting the
* user for a station name
*/
jtp.add(new Table(s));
jtp.setTitleAt(jtp.getTabCount()-1, s);
jtp.setSelectedIndex(jtp.getTabCount()-1);
}
public void addStation(Table t) {
jtp.add(t);
jtp.setTitleAt(jtp.getTabCount()-1,t.getStation());
}
public void removeAllStations() {
jtp.removeAll();
}
public void removeStation() {
/*
* Removes currently selected station
*/
jtp.remove(jtp.getSelectedIndex());
}
public void removeTrain() {
/*
* WIP: Removes a train
*/
Table t = (Table) jtp.getComponentAt(jtp.getSelectedIndex());
t.removeTrain();
}
}
Sounds like you want a javax.swing.Timer to fire every 30 seconds.
Alternatively, use a timer in a background thread (e.g. via a ScheduledExecutorService) - then fetch the information and only get the UI involved when you actually need to update the UI.
You've created a thread from the UI, which means that the thread will run with the same priority as the UI. You should not do that, but use SwingUtilities instead.
I am using BlueJ and i am trying to call a method from another class. To be more specific i am trying to complete the following.
When the download music button is pressed, if a suitable value has been entered for the display number:
The display number is used to get the gadget, cast as MP3, from the array list.
The method to download music in the MP3 class is called with the
download size entered.
Here is the gadgetshop class that builds the GUI and the place where i want to call the downloadMusic method. the method for the button is called downloadMusic.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.ArrayList;
public class GadgetShop implements ActionListener
{
//Array List
private ArrayList<Gadget>gadgets;
public void actionPerformed(ActionEvent event)
{
if (command.equals("Download Music"))
{
downloadMusic();
}
}
public void addMp3()
{
MP3 mp3 = new MP3(getWeight(), getPrice(), getModel(), getSize(), getMemory());
gadgets.add(mp3);
}
public void displayAll()
{
for(Gadget gadget : gadgets)
{
gadget.print();
System.out.println();
}
}
public void downloadMusic()
{
}
public int getDisplay()
{
int gadgetDisplay = 0;
try
{
gadgetDisplay = Integer.parseInt(displayText.getText());
if (gadgetDisplay<= 0)
{
JOptionPane.showMessageDialog
(frame, "Please enter a positive amount");
}
}
catch(NumberFormatException exception)
{
JOptionPane.showMessageDialog
(frame, "Please enter a positive amount");
}
return gadgetDisplay;
}
public String getDownload()
{
String gadgetDownload;
gadgetDownload = downloadText.getText();
return gadgetDownload;
}
}
This is the MP3 class
public class MP3 extends Gadget
{
private int memory;
public MP3(int theWeight, double thePrice, String theModel, String theSize, int theMemory)
{
super(theWeight,thePrice, theModel, theSize);
memory = theMemory;
}
public void downloadMusic(String music, int MusicSize)
{
if(MusicSize>memory)
//if statement saying if size is greater than memory then display the follwing statemnt saying there is not enough memory
{
System.out.println("Not Enough Memory");
}
else
// else statement opposite to the above statement saying if music size is less than or equal to the memory display the following statement
{
memory = memory - MusicSize;
System.out.println("Download Successfull. "+ "\nMusic Name: "+ music + "\nMemory Left: " + memory);
}
}
The "other class" (with the button) must have an instance of the class with the method you want to call. You could create an instance of MP3 in the constructor of GadgetShop and store it as an instance variable. Then in your button listener call
mp3Instance.downloadMusic("music", 42);
I have the following method within a thread, and it works 100%, but once I remove System.out.println("here"); the code stops doing what it was doing. There are no errors, it just has the appearance of not doing anything. What this does is brightens the colors of an image, with the debug line in it brightens, without it, it doesn't brighten. Why is it causing that?
The thread Class:
package pocketshop.threads;
import com.jogamp.opencl.CLBuffer;
import java.awt.Container;
import java.awt.image.BufferedImage;
import java.nio.FloatBuffer;
import pocketshop.Canvas;
import pocketshop.graphics.CL;
import pocketshop.graphics.Preview;
/**
*
* #author Ryan
*/
public class AdjustThread extends Thread {
protected float amount = 0;
protected CLBuffer<FloatBuffer> buffer;
protected String adjustment;
protected Container parent;
public AdjustThread(Container parent, String adjustment) {
this.parent = parent;
this.adjustment = adjustment;
}
public void setAmount(float amount){
this.amount = amount;
}
public CLBuffer<FloatBuffer> getBuffer() {
return buffer;
}
public void run() {
float cAmount = 0;
while(true){
System.out.println("here");
if(cAmount != this.amount){
cAmount = this.amount;
CL.start(adjustment, this.amount);
buffer = CL.getBuffer();
float[] pixels = CL.getPixels();
BufferedImage newimage = new BufferedImage(Canvas.image.getWidth(), Canvas.image.getHeight(), BufferedImage.TYPE_INT_RGB);
buffer.getBuffer().get(pixels).rewind();
newimage.getRaster().setPixels(0, 0, Canvas.image.getWidth(), Canvas.image.getHeight(), pixels);
Preview.setImage(newimage);
Canvas.preview = Preview.getImage();
parent.repaint();
}
}
}
}
And the Dialog box relevant code:
package pocketshop.dialogs;
import java.awt.image.BufferedImage;
import pocketshop.Canvas;
import pocketshop.graphics.adjustments.Contrast;
import pocketshop.threads.AdjustThread;
/**
*
* #author Ryan
*/
public class BrightnessContrastDialog extends javax.swing.JDialog {
AdjustThread adj;
/**
* Creates new form BrightnessContrastDialog
*/
public BrightnessContrastDialog(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();
adj = new AdjustThread(this.getParent(), "Brightness");
adj.start();
}
// Run everytime the JSlider moves
private void sldBrightnessStateChanged(javax.swing.event.ChangeEvent evt) {
float val = sldBrightness.getValue();
txtBrightness.setText("" + (int) val);
adj.setAmount(val);
}
}
Is this.amount declared as volatile?
I take it this field changes by virtue of assignment in another thread. If it's not declared as volatile, there's no reason to assume that the thread running the run() method above will ever observe a change to it. After the first time you assign this.amount to cAmount, they remain equal thereafter—from the point of view of this thread, anyway.
Once you clarify for us the declared qualifiers on this.amount and show the snippet of code where it's changed elsewhere, we can help specify the proper synchronization devices necessary to restore the behavior you desire.
As for why the call to PrintStream#println() seems to make a difference here, it's likely causing not only a delay but may also be hitting a happens-before memory visibility edge that's allowing the changes to this.amount to become visible to this thread. That's a lot of hand-waving, but I think that there are larger problems here to solve first before pegging the root cause for that particular side effect.
I'm making a 2D game in Java, I used the KeyListener and some Booleans to detect key press. But the problem is, whe I hold down a key the player wouldnt move for half a second, and then starts moving. Does anyone know how to fix this?
public void keyPressed(...)
{
PlayerX += 3;
}
Any answers would be appreciated thanks.
There are multiple ways to handle controls for games in java, but my prefereble way would include a class called.. lets say "Key.class"
Inside Key.class we can have:
public class Key{
// Creating the keys as simply variables
public static Key up = new Key();
public static Key down = new Key();
public static Key left = new Key();
public static Key special = new Key();
/* toggles the keys current state*/
public void toggle(){
isDown = !isDown;
}
public boolean isDown;
}
Now we have a class where we can access if certain keys are pressed, but first we need make sure that the keys .isDown function will be toggled properly. We do this in our class which implements KeyListener.
Let's say we have "Controller.class"
package game;
// Importing the needed packages
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.HashMap;
public class Controller implements KeyListener{
//Assigning the variable keys to actual letters
public Controller(Main main){
bind(KeyEvent.VK_W, Key.up);
bind(KeyEvent.VK_A, Key.left);
bind(KeyEvent.VK_S, Key.down);
bind(KeyEvent.VK_D, Key.right);
bind(KeyEvent.VK_SPACE, Key.special);
mainClass = main;
}
#Override
public void keyPressed(KeyEvent e) {
other[e.getExtendedKeyCode()] = true;
keyBindings.get(e.getKeyCode()).isDown = true;
}
#Override
public void keyReleased(KeyEvent e) {
other[e.getExtendedKeyCode()] = false;
keyBindings.get(e.getKeyCode()).isDown = false;
}
public boolean isKeyBinded(int extendedKey){
return keyBindings.containsKey(extendedKey);
}
#Override
public void keyTyped(KeyEvent e) {
}
public void bind(Integer keyCode, Key key){
keyBindings.put(keyCode, key);
}
public void releaseAll(){
for(Key key : keyBindings.values()){
key.isDown = false;
}
}
public HashMap<Integer, Key> keyBindings = new HashMap<Integer, Key>();
public static boolean other[] = new boolean[256];
}
Now this class will handle all of our keyBindings for us, and assuming you addKeyListener for the Canvas or whatever your game is running on it will function and change the Key.up/down/left/right/special accordingly.
Now the final step is to implement all of this into moving our character efficiently and easily.
Assuming your entities in game have update() methods which run every tick or something similar.. We can now simple add into it
if(Key.up.isDown) y+=3;
or in your case we could put it into the main class and do it the same way as long as its in the game tick loop.
if(Key.right.isDown) PlayerX += 3;
This sounds like a normal behavior of the repeat (auto-repeat) for key pressed in your OS. Just try holding a key down in any text editor and you will notice that there is a short amount of time between the first character being displayed and the next ones. On Windows this is 500ms, not sure on other platforms.
I have created a textfield that takes all characters from the user.. but i want to disable the space so that user cant enter space ...help??
pin = new TextField("Pin#","",4,TextField.PASSWORD);
If it's a PIN number then maybe you should replace the constraints parameter with TextField.NUMERIC | TextField.PASSWORD.
Implement the ItemStateListener interface. Then call this.setItemStateListener(this) in the Form constructor.
Implement the itemStateChanged method so that if the Item is the one you are interested in then get its content and test it if it contains spaces.
In my case I create a MIDlet and a Form which contains a TextField. And it works. I do not know why did you say that the solution I gave you did not work to you ! Here is a very simple example I give ( I created it and tested it ! ) :
package hello;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class HelloMIDlet extends MIDlet {
public Display display;
public HelloMIDlet() {
display = Display.getDisplay(this);
}
public void startApp() {
Form f = new F(display);
display.setCurrent(f);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
}
package hello;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemStateListener;
import javax.microedition.lcdui.TextField;
public class F extends Form implements ItemStateListener {
private TextField pin = new TextField("PIN :","",4,TextField.PASSWORD);
private Alert alert;
private Display disp;
public F(Display d)
{
super("");
disp = d;
this.setItemStateListener(this);
this.append(pin);
}
public void itemStateChanged(Item item) {
if (item == pin)
{
for (int i=0; i<pin.getString().length(); i++)
{
if (String.valueOf(pin.getString().charAt(i)).equals(new String(" ")))
displayAlert();
}
}
}
private void displayAlert()
{
alert = new Alert("Info","No space please !",null, AlertType.ERROR);
disp.setCurrent(alert, this);
}
}