This source will throw "NullPointerException" at "ans = NumFunctions.arrayConcInt(a);" StringBuilder that Declarationed at thread of "arrayConcInt" has null value.

Format
Java
Post date
2018-12-11 17:39
Zeitraum der Veröffentlichung
Unbegrenzt
  1. //hitAndBlow.java
  2. package com.hizumiaoba.hitandblow;
  3. import java.io.IOException;
  4. import java.util.Random;
  5. import org.lib.system.functions.NumFunctions;
  6. import org.lib.system.input.IntegerAuditCommittee;
  7. import org.lib.system.input.Numbers;
  8. public class Main
  9. {
  10. private static final long seed = System.currentTimeMillis() + Runtime.getRuntime().freeMemory();
  11. private static int LENGTH;
  12. private static int[] a;
  13. private static int num;
  14. private static int ans;
  15. private static int hit;
  16. private static int blow;
  17. public static void main(String args[]) throws Exception
  18. {
  19. Random rnd = new Random(seed);
  20. try
  21. {
  22. System.out.println("Enter length...");
  23. LENGTH = Numbers.InputInteger();
  24. }
  25. catch(IOException e)
  26. {
  27. e.printStackTrace();
  28. }
  29. for (int i = 0; i >= LENGTH - 1; i++)
  30. {
  31. a[i] = rnd.nextInt(9) + 1;
  32. }
  33. ans = NumFunctions.arrayConcInt(a);
  34. do
  35. {
  36. try
  37. {
  38. System.out.println("Please enter number...");
  39. num = Numbers.InputInteger();
  40. }
  41. catch(IOException e)
  42. {
  43. e.printStackTrace();
  44. }
  45. if (String.valueOf(num).length() > LENGTH)
  46. {
  47. System.out.println("Please enter the number which you specified");
  48. continue;
  49. }
  50. hit = IntegerAuditCommittee.numOfHits(num, ans);
  51. blow = IntegerAuditCommittee.numOfBlows(num, ans);
  52. if(IntegerAuditCommittee.isNumberCorrect(num, ans))
  53. break;
  54. try
  55. {
  56. Thread.sleep(100);
  57. }
  58. catch(InterruptedException e)
  59. {
  60. e.printStackTrace();
  61. }
  62. System.out.println("Your current status below.\n");
  63. System.out.println(hit + "hit" + blow + "blow");
  64. System.out.println("\nGood luck!\n");
  65. try
  66. {
  67. Thread.sleep(100);
  68. }
  69. catch(InterruptedException e)
  70. {
  71. e.printStackTrace();
  72. }
  73. }
  74. while(true);
  75. System.out.println("Congraturations!\n The number is" + ans + "\n\nThank you for playing!");
  76. System.exit(0);
  77. }
  78. }
  79. //NumFunction.java
  80. package org.lib.system.functions;
  81. import org.lib.exceptions.GetSpecificDigitException;
  82. public class NumFunctions
  83. {
  84. public static int getDigit(int argnum, int argdig) throws GetSpecificDigitException
  85. {
  86. int length = String.valueOf(argnum).length();
  87. if(length < argdig)
  88. throw new GetSpecificDigitException("Unexpected Digit Entered");
  89. int d = (int)Math.pow(10, length -1);
  90. int intcrntdig = 0;
  91. for(int i = 1; i <= length; i++)
  92. {
  93. intcrntdig = argnum / d;
  94. if(length - i + 1 == argdig)
  95. break;
  96. argnum %= d;
  97. d /= 10;
  98. }
  99. return intcrntdig;
  100. }
  101. public static int getLength(int num)
  102. {
  103. return String.valueOf(num).length();
  104. }
  105. public static int arrayConcInt(int[] arg) throws NumberFormatException
  106. {
  107. StringBuilder sb = new StringBuilder();
  108. for(int i = 0; i >= arg.length; i++)
  109. {
  110. sb.append(arg[i]);
  111. }
  112. return Integer.parseInt(sb.toString());
  113. }
  114. }
Download Printable view

URL of this paste

Embed with JavaScript

Embed with iframe

Raw text