Auf eine Methode von einer anderen Klasse aus ändern?

Ich will aus meiner Klasse "bot" auf eine Methode zugreifen, dass sich in "SchwarmView" befindet.
Jetzt hab ich mich schon bei vielen Foren durchgeklickt, aber die Art, wie da die Lösung beschrieben wurde, funktioniert bei mir irgendwie nicht.
So sieht das bisher aus:

Java:
package schwarm;
//stark gekürzt...
public class SchwarmView extends FrameView {
public void setgesamt(int Zahl){
int speicher;
speicher=Integer.valueOf(gesamt.getText())+Zahl;    
gesamt.setText(String.valueOf(speicher));    
}
}

die Klasse "bot"

Java:
package schwarm;
import schwarm.SchwarmView;
//auch stark gekürzt...
private SchwarmView schwarmview1;
schwarmview1.setgesamt(50);

Es wirft eine NullPointerException. So definieren: SchwarmView schwarmview1 = new SchwarmView(); klappt nicht.

Danke im voraus für Lösungsvorschläge 🙂
 
Code:
SchwarmView schwarmview1 = new SchwarmView();
klappt warum nicht? Hat die keinen public parameterlosen Konstruktor?
 
Java:
public class Test {

private int zahl;
public Test() { //Konstruktor
zahl = 5;
}

public void setZahl(int z) {
zahl = z;
System.out.println("Geänderter Wert für Variable zahl: "+zahl);
}

}


public class ZweiteKlasse {

public static void main(String[]args) {
Test test = new Test();
test.setZahl(50);
}
}

Hoffe das hilft dir ?
 
Code:
SchwarmView schwarmview1 = new SchwarmView();
klappt warum nicht? Hat die keinen public parameterlosen Konstruktor?

Der Konstruktor:

Java:
public class SchwarmView extends FrameView {
    private Liste botliste;
    public SchwarmView(SingleFrameApplication app) {
        super(app);
        initComponents();
        botliste=new Liste(); 
        animation1.set_botliste(botliste);
        // status bar initialization - message timeout, idle icon and busy animation, etc
        ResourceMap resourceMap = getResourceMap();
        int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
        messageTimer = new Timer(messageTimeout, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                statusMessageLabel.setText("");
            }
        });
        messageTimer.setRepeats(false);
        int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate");
        for (int i = 0; i < busyIcons.length; i++) {
            busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
        }
        busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
                statusAnimationLabel.setIcon(busyIcons[busyIconIndex]);
            }
        });
        idleIcon = resourceMap.getIcon("StatusBar.idleIcon");
        statusAnimationLabel.setIcon(idleIcon);
        progressBar.setVisible(false);

        // connecting action tasks to status bar via TaskMonitor
        TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
        taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                String propertyName = evt.getPropertyName();
                if ("started".equals(propertyName)) {
                    if (!busyIconTimer.isRunning()) {
                        statusAnimationLabel.setIcon(busyIcons[0]);
                        busyIconIndex = 0;
                        busyIconTimer.start();
                    }
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(true);
                } else if ("done".equals(propertyName)) {
                    busyIconTimer.stop();
                    statusAnimationLabel.setIcon(idleIcon);
                    progressBar.setVisible(false);
                    progressBar.setValue(0);
                } else if ("message".equals(propertyName)) {
                    String text = (String)(evt.getNewValue());
                    statusMessageLabel.setText((text == null) ? "" : text);
                    messageTimer.restart();
                } else if ("progress".equals(propertyName)) {
                    int value = (Integer)(evt.getNewValue());
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(false);
                    progressBar.setValue(value);
                }
            }
        });
        jLabel17.setVisible(false);
        undead.setVisible(false);
        jLabel18.setVisible(false);
        ZOMBIE.setVisible(false);
    }
 

Neue Themen


Zurück
Oben