Caused by: java.lang.StackOverflowError: stack size 1040KB at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:649)

huangapple go评论80阅读模式
英文:

Caused by: java.lang.StackOverflowError: stack size 1040KB at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:649)

问题

以下是您要翻译的代码部分:

我正在尝试从JSON对象动态构建查询使用`StringBuilder``Iterator`。

**方法**

```java
public static void main(String[] args) {
    // JSONArray jsonArray = json数组

    // for(int i=0;i<jsonArray.length();i++){
    // JSONObject jsonObject=jsonArray.get(i);
    // 用于示例的JSON
    JSONObject jsonObject = new JSONObject(
        "{\"SchemaId\":\"186f134a-e65c-4e2d-92c6-04afe6951dd6\",\"SchemaName\":\"CIR\",\"Schema\":\"{}\",\"Parent\":null,\"Application\":\"CA\",\"ModifiedDateTime\":\"2020-08-31T04:21:46.403\",\"Version\":\"3\",\"PageType\":\"Line\",\"favorite\":false,\"CategoryType\":\"bits\",\"IsDeleted\":true}"
    );
    createPreparedStmtQuery(jsonObject, "mytable");
    // }

}

public static String createPreparedStmtQuery(JSONObject record, String tableName) throws JSONException {
    record = modifyRecord(record);

    Iterator<String> keys = record.keys();
    StringBuilder builder = new StringBuilder();
    builder.append("insert or replace into ");
    builder.append(tableName);
    builder.append(" (");

    while (keys.hasNext()) {
        builder.append(keys.next()).append(",");
    }
    builder.setLength(builder.length() - 1);

    builder.append(") values(");
    keys = record.keys();
    JSArray valuesArray = new JSArray();
    while (keys.hasNext()) {
        String key = keys.next();
        if (record.get(key) != null && record.get(key) instanceof String) {

        }

        builder.append("?").append(",");
        valuesArray.put(record.get(key));
    }
    builder.deleteCharAt(builder.length() - 1);

    builder.append(");");

    JSONObject ret = new JSONObject();
    ret.put("query", builder.toString());
    ret.put("values", valuesArray);
    return ret.toString();
}

private static JSONObject modifyRecord(JSONObject record) throws JSONException {

    if (record.has("ValidFrom")) {
        record.remove("ValidFrom");
    }
    if (record.has("ValidTo")) {
        record.remove("ValidTo");
    }

    Iterator<String> keys = record.keys();
    while (keys.hasNext()) {
        String key = keys.next();
        if (record.get(key) instanceof JSONObject || record.get(key) instanceof List) {
            record.put(key, ((JSObject) record.get(key)).toString());
        }
    }

    return record;
}

以及类似这样的错误:

Caused by: java.lang.StackOverflowError: stack size 1040KB at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:649)

上述方法中是否存在问题?如何解决它?谢谢。


<details>
<summary>英文:</summary>
I am trying to build query dynamically from json object using `StringBuilder` and `Iterator`.
**method:**
public static void main(String[] args) {
//JSONArray jsonArray = array of json
//for(int i=0;i&lt;jsonArray.length();i++){
//JSONObject jsonObject=jsonArray.get(i);
//for sample json
JSONObject jsonObject=new JSONObject(&quot;{\&quot;SchemaId\&quot;:\&quot;186f134a-e65c-4e2d-92c6-04afe6951dd6\&quot;,\&quot;SchemaName\&quot;:\&quot;CIR\&quot;,\&quot;Schema\&quot;:\&quot;{}\&quot;,\&quot;Parent\&quot;:null,\&quot;Application\&quot;:\&quot;CA\&quot;,\&quot;ModifiedDateTime\&quot;:\&quot;2020-08-31T04:21:46.403\&quot;,\&quot;Version\&quot;:\&quot;3\&quot;,\&quot;PageType\&quot;:\&quot;Line\&quot;,\&quot;favorite\&quot;:false,\&quot;CategoryType\&quot;:\&quot;bits\&quot;,\&quot;IsDeleted\&quot;:true}&quot;);
createPreparedStmtQuery(jsonObject,&quot;mytable&quot;);
//}
}
public static  String createPreparedStmtQuery(JSONObject record,String tableName) throws JSONException {
record = modifyRecord(record);
Iterator&lt;String&gt; keys = record.keys();
StringBuilder builder = new StringBuilder();
builder.append(&quot;insert or replace into &quot;);
builder.append(tableName);
builder.append(&quot; (&quot;);
while(keys.hasNext()){
builder.append(keys.next()).append(&quot;,&quot;);
}
builder.setLength(builder.length() - 1);
builder.append(&quot;) values(&quot;);
keys = record.keys();
JSArray valuesArray = new JSArray();
while(keys.hasNext()){
String key = keys.next();
if( record.get(key) != null &amp;&amp; record.get(key) instanceof String) {
}
builder.append(&quot;?&quot;).append(&quot;,&quot;);
valuesArray.put(record.get(key));
}
builder.deleteCharAt(builder.length() - 1);
builder.append(&quot;);&quot;);
JSONObject ret = new JSONObject();
ret.put(&quot;query&quot;,builder.toString());
ret.put(&quot;values&quot;,valuesArray);
return ret.toString();
}
private static JSONObject modifyRecord(JSONObject record) throws JSONException {
if (record.has(&quot;ValidFrom&quot;)) {
record.remove(&quot;ValidFrom&quot;);
}
if (record.has(&quot;ValidTo&quot;)) {
record.remove(&quot;ValidTo&quot;);
}
Iterator&lt;String&gt; keys = record.keys();
while(keys.hasNext()){
String key = keys.next();
if (record.get(key) instanceof JSONObject || record.get(key) instanceof List) {
record.put(key,((JSObject)record.get(key)).toString());
}
}
return record;
}
**And an Error receiving like this:**
Caused by: java.lang.StackOverflowError: stack size 1040KB         at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:649)
Is there any issue in the above methods?How to solve it?
Thanks.
</details>
# 答案1
**得分**: 1
你可以尝试增加你的JVM堆栈大小。在你的情况下,大小似乎是1040KB。你可以将它设置为另一个值(例如48 MB),如下所示:
```bash
java -Xss48m

更多详细信息和示例请参阅https://www.baeldung.com/jvm-configure-stack-sizes或官方参考https://www.oracle.com/java/technologies/javase/vmoptions-jsp.html。

英文:

You could try to increase the stack size of your JVM. In your case, the size seems to be 1040KB. You can set it to another value (e.g. 48 MB) like this:

java -Xss48m

For more details and examples see https://www.baeldung.com/jvm-configure-stack-sizes or the official reference https://www.oracle.com/java/technologies/javase/vmoptions-jsp.html

huangapple
  • 本文由 发表于 2020年9月1日 16:21:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/63683914.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定