11 Replies - 958 Views - Last Post: 11 February 2016 - 11:45 AM Rate Topic: *---- 1 Votes

#1 Chieve   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 15-August 15

Need help with Javafx. Label overlaps and Null pointer exception?

Posted 03 February 2016 - 01:30 PM

I have been trying to teach myself Javafx for extra credit. For my class I am trying to make a simple UI model.

This is what I got so far

(please ignore number on top, I am just adding it in so incase my professor googles my code, they know I went for help online and not copy and pasted, it's my ID...)

//108608151

package teammanager;


import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.layout.GridPane;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.control.Label;
import javafx.scene.text.Text;



public class TeamManager extends Application{
@Override    
public void start(Stage primaryStage){
    Team team = new Team();
    GridPane pane = new GridPane();
    Button add = new Button("Add Player");
    Button stats = new Button("Get Player Stats");
    
    
    add.setOnAction((ActionEvent e)-> {
        Player p = new Player();
    GridPane newPane = new GridPane();
    Label nameLabel = new Label("Name:");
    TextField nameText = new TextField("Enter Name");
    Label posLabel = new Label("Position: ");
    TextField posText = new TextField("Enter Position of Player");
    Label hitLabel = new Label("Hits: ");
    TextField hitText = new TextField("Enter number of Hits");
    Label errorLabel = new Label("Errors: ");
    TextField errorText = new TextField("Enter number of Errors");
    Button submit = new Button("Submit");
    Button back = new Button("Back");
    
    back.setOnAction((ActionEvent) -> {
    start(primaryStage);
    });
    
    submit.setOnAction((ActionEvent) -> {
    int pos = Integer.parseInt(posText.getText());
    p.setName(nameText.getText());
    p.setHits(Integer.parseInt(hitText.getText()));
    p.setError(Integer.parseInt(errorText.getText()));
    team.addPlayer(p, pos);
    Text text = new Text("Successfully Added!");
    newPane.getChildren().add(text);
    newPane.setRowIndex(text, 7);
        /*
        add play hits and erros over here, not in the add player section.
        you can not make player constructor...
        */
   
    });
    newPane.getChildren().addAll(nameLabel, nameText, posLabel, posText, hitLabel, hitText, errorLabel, errorText, submit, back);
    newPane.setRowIndex(nameLabel, 1);
    newPane.setRowIndex(nameText, 2);
    newPane.setRowIndex(posLabel, 1);
    newPane.setColumnIndex(posLabel, 2);
    newPane.setRowIndex(posText, 2);
    newPane.setColumnIndex(posText, 2);
    newPane.setRowIndex(hitLabel, 3);
    newPane.setRowIndex(hitText, 4);
    newPane.setRowIndex(errorLabel, 3);
    newPane.setColumnIndex(errorLabel, 2);
    newPane.setRowIndex(errorText, 4);
    newPane.setColumnIndex(errorText, 2);
    newPane.setRowIndex(submit, 5);
    newPane.setColumnIndex(back, 2);
    newPane.setRowIndex(back, 5);
    Scene scene = new Scene(newPane, 500, 500);
    primaryStage.setScene(scene);
    primaryStage.show();
    });
            
            
            stats.setOnAction((ActionEvent e) -> {
            GridPane newPane = new GridPane();
            Label  posLabel = new Label("Enter Position: ");
            TextField posText = new TextField("Enter players Position");
           Button submit = new Button("Submit");
           Button back = new Button("Back");
           
           submit.setOnAction((ActionEvent eh) -> {
            
            
            int pos = Integer.parseInt(posText.getText());
            String info = team.getPlayer(pos).getName() + " Hits: " + team.getPlayer(pos).getHits() + "Errors: " + team.getPlayer(pos).getError();
                Label statInfo = new Label(info);
                newPane.getChildren().add(statInfo);
                newPane.setRowIndex(statInfo, 5);
               }); 
           back.setOnAction((ActionEvent er) -> {start(primaryStage);});
           
               newPane.getChildren().addAll(posLabel, posText, submit, back); 
               newPane.setRowIndex(posText, 2);
           newPane.setRowIndex(submit, 2);
           newPane.setColumnIndex(submit, 2);
           newPane.setRowIndex(back, 3);
               Scene scene = new Scene(newPane, 500, 500);
               primaryStage.setScene(scene);
               primaryStage.show();
            
            });
    
            
    
    
    pane.getChildren().addAll(add, stats);
    pane.setColumnIndex(stats, 2);
    Scene scene = new Scene(pane, 500, 500);
    primaryStage.setScene(scene);
    primaryStage.show();
    }
    
    
    public static void main(String[] args) {
        launch(args);
    }
    
} 


class Player extends TeamManager{
    private String name;
    private int hits;
    private int error;

public String getName(){
return name;
}

public void setName(String name){
this.name = name;
}

public int getHits(){
return hits;
}

public void setHits(int hits){
this.hits = hits;
}

public int getError(){
return error;
}

public void setError(int error){
this.error = error;
}

public String toString(){
return "Name: " + this.getName() + " Hits: " + this.getHits() + " Erros: " + this.getError();
}

} 


class Team extends Player implements Cloneable{
    private final int MAX_NUMBER_OF_PLAYERS = 40;
    private Player[] playerArray;
 
    
    public Team(){
    playerArray = new Player[MAX_NUMBER_OF_PLAYERS];
    
   
}
    
    public void addPlayer(Player p, int position){
   if (size() == 40)
       return;
   for(int i = size(); i > position; i--){
   this.playerArray[i] = this.playerArray[i - 1];
   }
   this.playerArray[position] = p;
    }
    
    @Override
    public Object clone(){
    Object o = this;
    try{
    o = (Object)super.clone();
    }catch(CloneNotSupportedException e){}
    return o;
    }
    
    public boolean equals(Object o){
        Team Obj = (Team) o;
    for (int i = 0; i < 40; i++){
        if (this.playerArray[i] != Obj.playerArray[i])
        return false;
    }
    return true;
    }
    
    public int size(){
    for (int i = 0; i <= 40; i++)
        if (this.playerArray == null)
            return i;
              return 40;      
    }
    
    public void removePlayer(int position){
   for (int i = position; i < size(); i++){
   this.playerArray[i] = this.playerArray[i + 1];
   }
   this.playerArray[size() - 1] = null;
    }
    
    public Player getPlayer(int position){
    return this.playerArray[position];
    }
    
    public Player getHitLeader(){
    Player best = null;
    for (int i = 0; i < this.size(); i++){
    if (this.playerArray[i].getHits() > best.getHits())
        best = this.playerArray[i];
    }
        
        return best;
    }
    
    public Player getErrorLeader(){
    Player best = null;
    for (int i = 0; i < this.size(); i++){
    if (this.playerArray[i].getHits() < best.getHits())
        best = this.playerArray[i];
    }
        
        return best;
    }
    
    public void printAllPlayers(){
    this.toString();
    } 
    
    public String toString(){
    String list = "";
        for (int i = 0; i < this.size(); i++){
    list = String.format("Position " , i , this.playerArray[i].getName() , " Hits: " , this.playerArray[i].getHits() , " Errors" , this.playerArray[i].getError() );
    
    }
       return list; 
    }
    
    
    
    
    }


run:
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
	at teammanager.TeamManager.lambda$null$3(TeamManager.java:94)
	at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
	at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
	at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
	at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
	at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
	at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
	at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
	at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
	at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
	at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
	at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
	at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
	at javafx.event.Event.fireEvent(Event.java:198)
	at javafx.scene.Node.fireEvent(Node.java:8411)
	at javafx.scene.control.Button.fire(Button.java:185)
	at com.sun.javafx.scene.control.behavior.Buttonbehavior.mouseReleased(Buttonbehavior.java:182)
	at com.sun.javafx.scene.control.skin.behaviorSkinBase$1.handle(behaviorSkinBase.java:96)
	at com.sun.javafx.scene.control.skin.behaviorSkinBase$1.handle(behaviorSkinBase.java:89)
	at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
	at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
	at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
	at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
	at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
	at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
	at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
	at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
	at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
	at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
	at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
	at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
	at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
	at javafx.event.Event.fireEvent(Event.java:198)
	at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
	at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
	at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
	at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
	at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:352)
	at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:275)
	at java.security.AccessController.doPrivileged(Native Method)
	at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$355(GlassViewEventHandler.java:388)
	at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
	at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:387)
	at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
	at com.sun.glass.ui.View.notifyMouse(View.java:937)
	at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
	at com.sun.glass.ui.win.WinApplication.lambda$null$149(WinApplication.java:191)
	at java.lang.Thread.run(Thread.java:745)

 


I did this all on netbeans. All in one program (which is why the team and player class don't have a "public" next to it).

I decided to add this in seperate sections to make this easier for you to view.

Please note there are a lot of extra code in there because I am not fully done creating my assignment.

So let me explain my first problem.

Under the add.setOnAction event, I have a label with a string "Successfully Added!" earlier I tried using

Text text = new Text(nameText.getName() + " was added successfully!"); 


just so who ever is grading my assignment can see that who ever that add into the array, it has worked.

However my first problem is, that the code would overlap names, so it will give it a weird look. so the name "Joe" and "Steven" would be on top of each other, and then the rest of the text gets messed up due to the varying sentence length.

The next problem is under the stats action event, when I go back to the menu to get player stats, it causes the error you can see in the 4th box of code I posted above. The stats label ends up not being posted onto the grid pane.

Can anyone help me out please?

This post has been edited by Chieve: 03 February 2016 - 01:31 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Need help with Javafx. Label overlaps and Null pointer exception?

#2 xclite   User is offline

  • I wrote you an code
  • member icon


Reputation: 1465
  • View blog
  • Posts: 4,317
  • Joined: 12-May 09

Re: Need help with Javafx. Label overlaps and Null pointer exception?

Posted 03 February 2016 - 03:16 PM

I'd start with looking at making your formatting (in particular, your indentation) consistent. The more organized your code is, the more likely somebody is to help you and the more easily that somebody (or you!) will find the problem.
Was This Post Helpful? 0
  • +
  • -

#3 Chieve   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 15-August 15

Re: Need help with Javafx. Label overlaps and Null pointer exception?

Posted 08 February 2016 - 09:20 PM

How do i remove my code? i fixed the issue and will like my code removed.

It was not solved here.

why can i edit this one but not my other one....

View Postxclite, on 03 February 2016 - 03:16 PM, said:

I'd start with looking at making your formatting (in particular, your indentation) consistent. The more organized your code is, the more likely somebody is to help you and the more easily that somebody (or you!) will find the problem.


damn it i meant to hit +

This post has been edited by Chieve: 08 February 2016 - 09:20 PM

Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101   User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12742
  • View blog
  • Posts: 45,926
  • Joined: 27-December 08

Re: Need help with Javafx. Label overlaps and Null pointer exception?

Posted 08 February 2016 - 09:24 PM

You can't remove your code, and we do not remove threads. (Please keep in mind this is against the rules.)

Perhaps you could share your solution so others can benefit. :)
Was This Post Helpful? 0
  • +
  • -

#5 Chieve   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 15-August 15

Re: Need help with Javafx. Label overlaps and Null pointer exception?

Posted 08 February 2016 - 09:52 PM

View Postmacosxnerd101, on 08 February 2016 - 09:24 PM, said:

You can't remove your code, and we do not remove threads. (Please keep in mind this is against the rules.)

Perhaps you could share your solution so others can benefit. :)/>


I will post my solution after my assignment has been graded so I won't have to worry about being accused of academic dishonesty.

This won't be for a week or two, because the homework is due thursday and it has to get graded.

To those who want help, incase I forget, or need help sooner can feel free to send me a PM.
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101   User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12742
  • View blog
  • Posts: 45,926
  • Joined: 27-December 08

Re: Need help with Javafx. Label overlaps and Null pointer exception?

Posted 08 February 2016 - 10:02 PM

Please keep all correspondence for help on the forums rather than the PM system.
Was This Post Helpful? 0
  • +
  • -

#7 Chieve   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 15-August 15

Re: Need help with Javafx. Label overlaps and Null pointer exception?

Posted 10 February 2016 - 09:34 AM

Please take this down now. I do not want to have to ask again.

No one has benefited from my topic. Nothing anyone told me on here helped resolve my problem. I had to go find help somewhere else.

The only benefit people will get from this is either googling the homework assignment before the assignment is due and copying and pasting my work.

I do not care if it is against the rules to remove topics. Some of this info should now be removed. I came to seek help which no one got.

If I have to get a ban in order for the topic to be removed so be it.

I am getting aggravated about how I can not have this remove. This is my personal work I don't want to worry about people copying and pasting. Some of this work is functional except for an error here and there. Even if I were to fix the error I can't because I can not edit my post.

This was my raw work, not finished work, but it was still my work that can cause me to get accused for academic dishonesty and I do not want to worry about myself being accused.
Was This Post Helpful? 0
  • +
  • -

#8 xclite   User is offline

  • I wrote you an code
  • member icon


Reputation: 1465
  • View blog
  • Posts: 4,317
  • Joined: 12-May 09

Re: Need help with Javafx. Label overlaps and Null pointer exception?

Posted 10 February 2016 - 09:37 AM

I'll save you the energy - asking again won't change things, nor will your getting banned. I hope the help you got in other places was useful, but here we want you to learn how to help yourself, which is why we don't shower answers down on posters from on high.
Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101   User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12742
  • View blog
  • Posts: 45,926
  • Joined: 27-December 08

Re: Need help with Javafx. Label overlaps and Null pointer exception?

Posted 10 February 2016 - 09:48 AM

This is an excerpt of the Terms of Service to which you agreed upon signing up for an account. What this means is that once you post something, we have the right to keep it posted and display it as we wish. And frankly, even if we removed it, your thread is already indexed and cached by Google and many other search engines. So your instructor and plagiarism detection software could likely find your code anyways. The bottom line is that once you post something online, it's out there. Think before your post.

Quote

Property of DREAMINCODE.NET: You agree that content created by this website through the use of posts/threads, snippets, tutorials, and FAQ and/or polls remains the property of this website and may be published by Dream.In.Code in other media forms. The owner is not responsible for any and all Messages expressed in the Forum. The owner does not attempt to control or validate any comments or recommendations provided by the participants and is not responsible for any inaccuracies in the Messages. Therefore, you bear the risk associated with, the accuracy, completeness or usefulness of the Messages.

Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101   User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12742
  • View blog
  • Posts: 45,926
  • Joined: 27-December 08

Re: Need help with Javafx. Label overlaps and Null pointer exception?

Posted 11 February 2016 - 08:22 AM

If you make a post, we have the right to display it forever. That's what you agreed too.

But since this thread has gotten off topic, I'll close it.
Was This Post Helpful? 0
  • +
  • -

#11 astonecipher   User is offline

  • Enterprise Software Architect
  • member icon

Reputation: 3131
  • View blog
  • Posts: 11,933
  • Joined: 03-December 12

Re: Need help with Javafx. Label overlaps and Null pointer exception?

Posted 11 February 2016 - 11:30 AM

Let me point out how academic dishonesty works for you,

That is when you steal someone else's work, not when you fix your own. Being as the forums have a timestamp and user associated with each post, that clearly says that you posted it on this day at this time, it proves IP at a specific point by a specific user. THEREFORE, the only way you would have to worry about a claim against you, is if you stole the original code.
Was This Post Helpful? 0
  • +
  • -

#12 macosxnerd101   User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12742
  • View blog
  • Posts: 45,926
  • Joined: 27-December 08

Re: Need help with Javafx. Label overlaps and Null pointer exception?

Posted 11 February 2016 - 11:45 AM

Actually, if the rules of the assignment prohibit seeking third party assistance, then him posting here would be an academic integrity violation as well.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1