Coder Profile - Show off your skills, get a coder profile.
 
 
 
  
Posted: 1.95 Year Ago 

Anto
Finland
Contrib Level: 5
Total Posts: 202
I'm creating a system for creating, managing and getting data aswell as drawing tables in a CLI using Java. So now I have experienced a problem, my sources are:
CODE: Table.java Copy / Restore  ::  Remove Scroll Bars
  1. package drawtable;
  2.  
  3. public class Table {
  4.  
  5.     private int rows = 1, columns = 1;
  6.     boolean borders = true;
  7.     String[][] data;
  8.  
  9.     public Table(int row, int col) {
  10.         this.rows = row;
  11.         this.columns = col;
  12.         this.createArray();
  13.     }
  14.  
  15.     public Table(int row, int col, boolean border) {
  16.         this.rows = row;
  17.         this.columns = col;
  18.         this.borders = border;
  19.         this.createArray();
  20.     }
  21.  
  22.     public void createArray() {
  23.         data = new String[this.rows + 1][this.columns + 1];
  24.     }
  25.  
  26. public void insertDataAt(int row, int col, String value) {
  27. data[row][col] = value;
  28. }
  29.  
  30. public String getDataFrom(int row, int col) {
  31. return data[row][col];
  32. }
  33.  
  34. public int getRows() {
  35. return rows;
  36. }
  37.  
  38. public int getColumns() {
  39. return columns;
  40. }
  41.  
  42. public boolean getBorders() {
  43. return borders;
  44. }
  45.  
  46. public void setBorders(boolean visible) {
  47. borders = visible;
  48. }
  49.  
  50. public void clearCell(int row, int col) {
  51. data[row][col] = null;
  52. }
  53.  
  54. public void clearAllCells() {
  55. }
  56.  
  57. public int[] findCellContaining(String text) {
  58. int i = 0, j = 0;
  59. int pos[] = new int[1];
  60. String temp;
  61. for (i = 1; i < this.getRows(); i++) {
  62. for (j = 1; j < this.getColumns(); j++) {
  63. temp = data[i][j];
  64. if (temp.contains(text)) {
  65. pos[0] = i;
  66. pos[1] = j;
  67. }
  68. }
  69. }
  70. String int_str = Integer.toString(pos[0]);
  71. if (int_str == null) {
  72. pos[0] = -1;
  73. pos[1] = -1;
  74. }
  75. return pos;
  76. }
  77.  
  78. public void printTable() {
  79. int i;
  80. for (i = 1; i < this.getRows() + 1; i++) {
  81. int j;
  82. for (j = 1; j < this.getColumns() + 1; j++) {
  83. if (borders) {
  84. System.out.print("|");
  85. }
  86. System.out.print(data[i][j] + "\t");
  87. }
  88. System.out.println();
  89. if (borders) {
  90. int k;
  91. for (k = 0; k < this.getColumns() + 1; k++) {
  92. System.out.print("-----------");
  93. }
  94. System.out.println();
  95. }
  96. }
  97. }
  98.  
  99. public int getLenghtOfLongestString() {
  100. int i;
  101. int longestString = 0;
  102. for (i = 1; i < this.getRows() + 1; i++) {
  103. int j;
  104. for (j = 1; j < this.getColumns() + 1; j++) {
  105. if ((data[i][j].length()) > longestString) {
  106. longestString = data[i][j].length();
  107. }
  108. }
  109. }
  110. return longestString;
  111. }
  112. }
CODE: main method for testing Copy / Restore  ::  Remove Scroll Bars
  1. package drawtable;
  2. public class Main {
  3.     public static void main(String[] args) {
  4.         Table table=new Table(8,7,true);
  5.         table.insertDataAt(8, 7, "Hello World!");
  6.         table.insertDataAt(3, 4, "joke");
  7.         table.insertDataAt(1, 2, "This is some additional data");
  8.         System.out.println(table.getDataFrom(1, 1));
  9.         table.printTable();
  10.         System.out.println(table.getLenghtOfLongestString());
  11.         int[] position=table.findCellContaining("joke");
  12.         int i;
  13.         for(i=0;i<1;i++){
  14.             System.out.println(position[i]);
  15.         }
  16.     }
  17. }
Both table.getLenghtOfLongestString() and table.findCellContaining() seem to cause NullPointerExceptions. Running the debugger on findCellContaining suggests that the String known as temp disappears (this extra variable was added for debugging). I'm really confused, but I believe there is a Java genious on these boards able to help me..? Very appreciated.

Forgot to say, the NullPointerException occurs at the if() sentances at both problematic methods
Page 1 of 1
 
 
Latest News About Coder Profile
Coder Profile Poll
If you made money from keeping your profile up to date, say $30 and up, per month. What extra time would you spend on your profile?

No extra time
A few hours at weekends
A whole day each week
Every minute i can get free


please login to cast your vote
and see the results of this poll
Latest Coder Profile Changes
Coder Profile was last updated
3.49 Years Ago
Official Blog :: Make A Donation :: Credits :: Contact Me
Terms & Conditions :: Privacy Policy :: Documents :: Wallpapers
Version 1.46.00
Copyright © 2007 - 2012, Scott Thompson, All Rights Reserved