[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 552
  • Last Modified:

Why does JTable throw null pointer on getselectedrow()?

In a Model View Controller application I have a JTABLE that loads and displays fine. However, as soon as I check to see what row is selected it throws a null pointer. Below is the code from the "View" and the null pointer is thrown at getMsgIndex(). I have also included the "Controller" code that listens for the button click and requests getMsgIndex(). I know I don't have a scrollpane and currently I only have 1 column of data.

Why is this thowing a null pointer?

// ===================View Code ============================

		JPanel card2 = new JPanel();
		card2.setBackground(Color.white);
		card2.setLayout(new BorderLayout());
		MyTableModel tableModel = new MyTableModel(getData());
		JTable msgActions = new JTable(tableModel);
		msgActions.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		msgActions.setColumnSelectionAllowed(true);
		msgActions.setRowSelectionAllowed(true);
		//		msgActions.setCellSelectionEnabled(true);
		msgActions.setShowGrid(false);
		msgActions.setTableHeader(null);
		msgActions.changeSelection(0, 0, true, false);
		card2.add(msgActions, BorderLayout.NORTH);
		optionsView.add(card2,cardNames[1]);

	}

	private void skipGrid (int skipCount, JPanel panel) {
		for(int i=0; i<skipCount; i++) {
			panel.add(new JLabel(""));
		}
	}

	public void setTimeField(JLabel timeField) {
		this.timeField = timeField;
	}

	public JLabel getTimeField() {
		return timeField;
	}
	public void setCardVisible(String card) {
		optionsLayout.show(optionsView, card);

	}
	public int[] getMsgIndex() {
		int row = msgActions.getSelectedRow();
		int col = msgActions.getSelectedColumn();
		int[] index = {row,col};
		return index;
	}
	public void setMsgIndex(int row, int col) {
		boolean toggle = false;
		boolean extend = false;
		msgActions.changeSelection(row, col, toggle, extend);
	}
	public int[] getLastMsgIndex() {
		int[] count = {msgActions.getRowCount(), msgActions.getColumnCount()};
		return count;
	}
	public String getCurrentCard() {
		// get the current card
		Component c[] = optionsView.getComponents();
		int i = 0;
		int j = c.length;
		while (i < j) {
			if (c[i].isVisible()) {
				return cardNames[i];
			}
			else
				i ++;
		}
		return null;
	}

	private Vector getData() 
	{ 
		Vector actionData = new Vector(10, 10); 
		for (int i=0; i<msgActionNames.length; i++) {
			actionData.addElement(new ActionData((msgActionNames[i] + "=" + msgCount[i])));
		}
		return actionData; 
	} 


	public class MyTableModel extends AbstractTableModel 
	{ 
		//Columns Number. 
		public static final int  ACTION_COL = 0;                                    

		//Names of the columns 
		public String[] m_colNames = {"Action"}; 
		// Types of the columns. 
		public Class[]  m_colTypes = {String.class}; 

		//store the data 
		Vector m_DataVector; 

		public MyTableModel(Vector dataVector) 
		{ 
			super(); 

			//store the data 
			m_DataVector = dataVector; 
		} 

		public int getColumnCount() 
		{   
			return m_colNames.length; 
		} 

		public int getRowCount()     
		{   
			return m_DataVector.size(); 
		} 


		public void setValueAt(Object value, int row, int col)  
		{     
			ActionData actionData =  (ActionData) (m_DataVector.elementAt(row)); 

			switch(col) 
			{ 
			case ACTION_COL : actionData.setActionName((String) value); 
			break; 
			} 
		} 


		public String getColumnName(int col) 
		{ 
			return m_colNames[col]; 
		} 

		public Class getColumnClass(int col)  
		{ 
			return m_colTypes[col]; 
		} 

		public Object getValueAt(int row, int col) 
		{ 
			ActionData actionData =  (ActionData) m_DataVector.elementAt(row); 

			switch(col) 
			{ 
			case ACTION_COL : return actionData.getActionName(); 
			} 

			return new String(); 
		} 
	} 
	public class ActionData    { 
		private String m_actionName;

		public ActionData() 
		{ 
		} 

		public ActionData(String actionName) 
		{ 
			m_actionName = actionName; 
		} 

		public String getActionName() 
		{ 
			return m_actionName; 
		} 

		public void setActionName(String actionName) 
		{ 
			m_actionName = actionName; 
		} 

	}

// ================ Controller Code ======================
	class NavPadListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			if ("up".equals(e.getActionCommand())) {
				 int[] rowCol =  m_view.getMsgIndex();
				   if(rowCol[0]== 0) return;
				   else
				    m_view.setMsgIndex(rowCol[0]-1, rowCol[1]);
			}
			if ("down".equals(e.getActionCommand())) {
				 int[] rowCol =  m_view.getMsgIndex();
				 int[] maxRowCol = m_view.getLastMsgIndex();
				 if (rowCol[0]==-1){
					 return;
				 }
				   if(rowCol[0]== maxRowCol[0]) return;
				   else
				    m_view.setMsgIndex(rowCol[0]+1,rowCol[1]);
			}
			if ("ok".equals(e.getActionCommand())) {
				return;
			}
		}

Open in new window

0
ABlahus
Asked:
ABlahus
  • 2
1 Solution
 
CEHJCommented:
Make sure msgActions is an instance variable, and don't hide it with

>>JTable msgActions = new JTable(tableModel);

It must be

msgActions = new JTable(tableModel);
0
 
objectsCommented:
>             JTable msgActions = new JTable(tableModel);

your creating a local var, so u don't actually initialize the member var
should be:

msgActions = new JTable(tableModel);
0
 
CEHJCommented:
:)

Check your other variables too
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now