NullPointerException Error with JPanel and CardLayout

后端 未结 1 1178
挽巷
挽巷 2021-01-26 13:42

I am working on hotel management software for class and I am running into a few issues with my code. At this point, I am simply trying to add a JPanel I created in a separate cl

相关标签:
1条回答
  • 2021-01-26 14:19
    jpanel.add("Room", room.getRoomPanel());
    

    You've never initialized room

    RoomSystem room;
    

    Even if you do initialize it RoomSystem room = new RoomSystem(), you still have another problem in your RoomSystem class. You have shadowed roomPanel, and therefore the class member is null, when you try and call getRoomPanel(). In your constructor, change

    // shadowing the class field roomPanel
    JPanel roomPanel = new JPanel(new FlowLayout());  
    
    to 
    
    roomPanel = new JPanel(new FlowLayout());
    
    0 讨论(0)
提交回复
热议问题