-2

I am trying to store a String into a column with datatype Big Decimal.

String a = transaction.getBeneAmt();
log.info(a);
tran.setTransAmt(new BigDecimal(transaction.getBeneAmt()));
tranRepository.saveAndFlush(trangloTran); //save into database

But I get

2017-07-05 18:04:19 [http-nio-8080-exec-1] INFO  ApiController - IDR 3,000,000.00
2017-07-05 18:04:19 [http-nio-8080-exec-1] ERROR o.a.c.c.C.[.[.[.[dispatcherServlet] - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NumberFormatException] with root cause
java.lang.NumberFormatException: null
    at java.math.BigDecimal.<init>(BigDecimal.java:494)
    at java.math.BigDecimal.<init>(BigDecimal.java:383)
    at java.math.BigDecimal.<init>(BigDecimal.java:806)
cнŝdk
  • 31,391
  • 7
  • 56
  • 78
John
  • 684
  • 11
  • 35
  • John, you really want to convert **Big Decimal to String**? As I'm able to see in your code that you are trying to convert **String to Big Decimal** – Mayank Jain Jul 05 '17 at 10:20
  • How much of your log entry is the value of `a`? (Does it include the IDR?) I don't think BigDecimal can handle thousands separators though. – Jon Skeet Jul 05 '17 at 10:20
  • https://stackoverflow.com/questions/13900204/bigdecimal-to-string Similar Question. – Liam Gaskell Jul 05 '17 at 10:20
  • @MayankJain post updated. – John Jul 05 '17 at 10:22
  • I get value `IDR 3,000,000.00`. No idea why will get `java.lang.NumberFormatException: null` – John Jul 05 '17 at 10:22
  • 1
    @Andreas: NumberFormatException != NullPointerException. – Jon Skeet Jul 05 '17 at 10:22
  • 1
    Well why did you expect `BigDecimal(String)` to be able to parse that? Did you expect it to just remove the leading "IDR " part? Does the documentation suggest it will do that? – Jon Skeet Jul 05 '17 at 10:23
  • @JonSkeet Yeah, sorry, I just noticed that too, and was about to undo it, but you did it for me. Thanks. – Andreas Jul 05 '17 at 10:23
  • "The string representation consists of an optional sign, '+' ( '\u002B') or '-' ('\u002D'), followed by a sequence of zero or more decimal digits ("the integer"), optionally followed by a fraction, optionally followed by an exponent." Basically, this is behaving as documented - if you pass in a value which doesn't follow the expected format, it throws NumberFormatException. – Jon Skeet Jul 05 '17 at 10:24
  • @JonSkeet so it is impossible to store Big Decimal with IDR xxxxx ? – John Jul 05 '17 at 10:25
  • @John yes, it won't be possible. – cнŝdk Jul 05 '17 at 10:27
  • 1
    @John `BigDecimal` is for *numbers*, e.g. `3000000.00` is a valid number. `3,000,000.00` is not a valid number for `BigDecimal`. The [Indonesian Rupiah](https://en.wikipedia.org/wiki/Indonesian_rupiah) `IDR` currency prefix is not a valid part of a *number* either. So no, `BigDecimal` cannot store an **amount** value with currency. – Andreas Jul 05 '17 at 10:27
  • Well yes, `BigDecimal` is a number. Where does "IDR" fit into that? You can store the *numeric* part of course - you'll want to strip off the "IDR" and remove the commas. – Jon Skeet Jul 05 '17 at 10:27
  • 1
    @John, did you tried solution provided [here](https://stackoverflow.com/a/3752616/5059727) but that will not help you to store **IDR** – Mayank Jain Jul 05 '17 at 10:30
  • thanks guys ... :) – John Jul 05 '17 at 10:41

1 Answers1

1

The problem here is that your String contains alphabetic characters so it won't be possible to convert it to BigDecimal, that's why you got NumberFormatException.

You need to replace these alphabetic characters before trying to convert it:

String a = transaction.getBeneAmt();
log.info(a);
tran.setTransAmt(new BigDecimal(a.replaceAll("[a-zA-Z,]", "").trim()));
tranRepository.saveAndFlush(trangloTran);
cнŝdk
  • 31,391
  • 7
  • 56
  • 78
  • Still won't work, since `" 3,000,000.00"` is not a valid value for [`new BigDecimal(String)`](https://docs.oracle.com/javase/8/docs/api/java/math/BigDecimal.html#BigDecimal-java.lang.String-). – Andreas Jul 05 '17 at 10:31
  • @Andreas Yes I missed the `,` part. Thank you I edited the answer – cнŝdk Jul 05 '17 at 10:32