Newbie Question: Multidimensional NSArrays, NullPointerExceptionErrors and Table drawing issues
Newbie Question: Multidimensional NSArrays, NullPointerExceptionErrors and Table drawing issues
- Subject: Newbie Question: Multidimensional NSArrays, NullPointerExceptionErrors and Table drawing issues
- From: William Jamieson <email@hidden>
- Date: Tue, 15 May 2001 23:45:27 +1000
Hi all!
Thanks to everybody for their speedy and informative responses regarding
makeshift Multidimensional NSMutableArrays. I can now set them up, add
rows, subtract rows, extract & replace data. It Rocks!!
Let me state before I begin that I am a complete newbie to Obj-oriented
programming (6 days and counting) so please feel free to tell me that I have
missed the point completely. That having been said . . . .
Whilst the Multidimensional NSMutableArrays are great, I am getting some
strange results from an NSTableView I have in a window. Sometimes when
clicking on a row (especially an newly created row) I get a
NullPointException Error. After one or several of these have occurred the
NSTableView9s rendering gets a bit scrambled and sometimes won't display the
column headers and or scrollbars and data.
One of the NullPointException Errors that I am getting is occurring on the
following line in the tableViewObjectValueForLocation() method:
String domainArrayDatabaseValue =
(String)domainArrayDatabaseRow.objectAtIndex(columnIndex);
The other thing I can9t figure out is how to make my NSTableView separate
the cells with a grid. The documentation says that the default setting for
a NSTableView is a grey visible grid but I can9t get one to show up.
If anyone would like to download a copy of my mini test project to see the
hideous code showcasing these problems they can download it at:
http://www.envision-systems.com.au/ServerTool.sit 600k
For those who wish to save themselves a download (small though it is) I have
included the custom class I created as the datasource for my NSTableView
below.
Any help would be greatly appreciated!!!!!!
Thanks for you time.
William Jamieson
Systems Developer
Smart Works Systems
_____________________________________________________________
113 Ferrars St Phone: 03) 9696 6851
Southbank Fax: 03) 9690 0092
VIC 3006 Mobile: 0412 715 735
Australia Email: email@hidden
WWW:
http://www.smartworks.com.au/
_____________________________________________________________
//--------------------------------------------------------------------------
------------//
//
// DomainListDataSource.java
// ServerTool
//
//
import com.apple.cocoa.foundation.*;
import com.apple.cocoa.application.*;
public class DomainListDataSource {
//--------------------------------------------------------------------------
------------//
// Global Variables
//
//--------------------------------------------------------------------------
------------//
// Domain List Table
NSTableView preferencesWindowTableDomainList;
// Domain List Table Columns
int columnCount = 4;
NSTableColumn preferencesWindowTableDomainColumnActive;
NSTableColumn preferencesWindowTableDomainColumnDomain;
NSTableColumn preferencesWindowTableDomainColumnUpdateDNS;
NSTableColumn preferencesWindowTableDomainColumnEmail;
// Domain Array
NSMutableArray domainTableDataArray = new NSMutableArray();
String newCellValue = "";
//--------------------------------------------------------------------------
------------//
// DomainListDataSource Constructor
//
//--------------------------------------------------------------------------
------------//
public DomainListDataSource() {
System.out.println("Constructing DomainListDataSource");
// Create emptyDomainRecord NSMutableArray
System.out.println("Creating 1 empty domain record in
domainTableDataArray");
System.out.println("Adding 1 empty record to domainTableDataArray");
}
//--------------------------------------------------------------------------
------------//
// addRecordToDomainListTable()
//--------------------------------------------------------------------------
------------//
public void addRecordToDomainListTable() {
//System.out.println("addRecordToDomainListTable() executed");
System.out.println("Adding new row at index " +
domainTableDataArray.count());
// Construct an empty domain record
NSMutableArray emptyDomainRecord = new NSMutableArray();
int loopcount = 0;
for(loopcount=0;loopcount<columnCount;loopcount++){
if(loopcount==1) {
emptyDomainRecord.addObject("apple.com");
}else{
emptyDomainRecord.addObject("xxx");
}
}
// Add the empty domain record to the domainTableDataArray as a new
empty row
domainTableDataArray.addObject(emptyDomainRecord);
preferencesWindowTableDomainList.noteNumberOfRowsChanged();
preferencesWindowTableDomainList.reloadData();
int indexOfLastRow = (domainTableDataArray.count() - 1);
preferencesWindowTableDomainList.selectRow(indexOfLastRow,false);
}
//--------------------------------------------------------------------------
------------//
// numberOfRowsInTableView()
//--------------------------------------------------------------------------
------------//
public int numberOfRowsInTableView(NSTableView aTableView) {
//System.out.println("numberOfRowsInTableView() executed");
// On initialisation of the window, if there are 0 rows in the
table add 1 row to stop the
// exception error.
if (domainTableDataArray.count()== 0) {
addRecordToDomainListTable();
}
return domainTableDataArray.count();
}
//--------------------------------------------------------------------------
------------//
// getNumberOfRowsInTableView()
//--------------------------------------------------------------------------
------------//
public int getNumberOfRowsInTableView() {
//System.out.println("getNumberOfRowsInTableView() executed");
return domainTableDataArray.count();
}
//--------------------------------------------------------------------------
------------//
// tableViewObjectValueForLocation() This method is activated when a
cell's contents is redrawn.
//--------------------------------------------------------------------------
------------//
public Object tableViewObjectValueForLocation(NSTableView theTable,
NSTableColumn theColumn, int rowIndex) {
//System.out.println("tableViewObjectValueForLocation() executed");
// Set columnIndex to the cell's column index
int columnIndex = getColumnIndex(theColumn);
// set domainArrayDatabaseRow to the extracted row that is required
NSMutableArray domainArrayDatabaseRow =
(NSMutableArray)domainTableDataArray.objectAtIndex(rowIndex);
// set domainArrayDatabaseValue to the database value of the cell
required
System.out.println("Row " + rowIndex + " column " + columnIndex);
String domainArrayDatabaseValue =
(String)domainArrayDatabaseRow.objectAtIndex(columnIndex);
return domainArrayDatabaseValue;
}
//--------------------------------------------------------------------------
------------//
// tableViewSetObjectValueForLocation()
// This method is activated when the user exits a cell.
// This method is invoked immediately after ServerToolController.java's
// tableViewShouldEditLocation() delagate method for the Domain Table.
// It updates the domain Database as well as the NSTable view when
// a cell is exited.
//--------------------------------------------------------------------------
------------//
public Object tableViewSetObjectValueForLocation(NSTableView aTableView,
Object replacementCellData, NSTableColumn theColumn, int rowIndex) {
System.out.println("tableViewSetObjectValueForLocation() executed");
// Set newCellValue to the exited cell's string value
newCellValue = (String)replacementCellData.toString();
// get theColumn's Index value
int columnIndex = getColumnIndex(theColumn);
// Replace Database cell data with exited cell's replacement data
System.out.println("Cell data on row " + rowIndex + ", column " +
columnIndex + " to be replaced with the value \"" + newCellValue + "\"");
// Extract the applicable row from the database
NSMutableArray domainArrayDatabaseRow =
(NSMutableArray)domainTableDataArray.objectAtIndex(rowIndex);
// Replace the applicable value in it.
domainArrayDatabaseRow.replaceObjectAtIndex(columnIndex,newCellValue);
// Replace the old table row with the updated row.
domainTableDataArray.replaceObjectAtIndex(rowIndex,domainArrayDatabaseRow);
preferencesWindowTableDomainList.noteNumberOfRowsChanged();
preferencesWindowTableDomainList.reloadData();
return null;
}
//--------------------------------------------------------------------------
------------//
// getColumnIndex() This method is retrieves the Index value of the column
passed to it.
//--------------------------------------------------------------------------
------------//
public int getColumnIndex(NSTableColumn theColumn) {
//System.out.println("getColumnIndex() executed");
// Set columnIndex to the exited cell's column index
int columnIndex = 0;
if(theColumn==preferencesWindowTableDomainColumnActive){columnIndex
= 0;
}else
if(theColumn==preferencesWindowTableDomainColumnDomain){columnIndex = 1;
}else
if(theColumn==preferencesWindowTableDomainColumnUpdateDNS){columnIndex = 2;
}else
if(theColumn==preferencesWindowTableDomainColumnEmail){columnIndex = 3;
}
return columnIndex;
}
//--------------------------------------------------------------------------
------------//
// displayRowValues() This method is used as a debugging tool to display
the values in a row.
//--------------------------------------------------------------------------
------------//
public void displayRowValues(NSMutableArray theRow) {
//System.out.println("displayRowValues() executed");
System.out.println("Objects in the row are {" +
theRow.objectAtIndex(0) + "," +
theRow.objectAtIndex(1) + "," +
theRow.objectAtIndex(2) + "," +
theRow.objectAtIndex(3) + "}. " +
"There are now " + domainTableDataArray.count() + " Rows in the
domainTableDataArray.");
}
//--------------------------------------------------------------------------
------------//
}