在Java中在类之间传递空字符串的问题

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

Problem with Passing Null String Between Classes in Java

问题

我正在尝试将一个字符串从一个类传递到另一个类,但是它传递的不是我所需的字符串,而是传递了一个空值,这导致我正在尝试执行的解密函数中出现错误。以下是我尝试实现的两个类的详细信息:

MainActivity -> 尝试获取我从 "String decrypted1 = d1.encryppass(encrypted);" 得到的值

                    Decrypt d1 = new Decrypt();
                try {
                    if (cursor.moveToFirst()) {
                        do {
                            String name = cursor.getString(0);
                            String encrypted = cursor.getString(1);
                            d1.myMethod(this);
                            decrypted1 = d1.encryppass(encrypted);
                            Log.i("Title:     ", name);
                            Log.i("Password:  ", decrypted1);
                            Toast.makeText(getApplicationContext(), decrypted1, Toast.LENGTH_LONG).show();
                        } while (cursor.moveToNext());
                    }
                } catch (NullPointerException exception) {
                    exception.printStackTrace();
                }
            }

Decryption2类 -> 并将上述语句传递到这里 "private String pinToMd5 = d3.decrypted1;"

public class Decrypt2 {
    private Cipher cipher;
    MainActivity d3;
    private String pinToMd5;
    public String pin_to_md5;
    SecretKeySpec secretKeySpe = new SecretKeySpec(this.pin_to_md5.getBytes(), "AES");

    public Decrypt2(MainActivity mainActivity) {
        d3 = mainActivity;
        pinToMd5 = d3.decrypted1;
        pin_to_md5 = MD5(pinToMd5);
    }
英文:

I'm trying to pass a string from one class to another, but instead of it passing the string I need, it's passing a null value, which causes an error in the decryption function I'm trying to do. Here are the two classes with details on each one that I'm trying to achieve:

MainActivity -> trying to take the value I get from "String decrypted1 = d1.encryppass(encrypted);"

                    Decrypt d1 = new Decrypt();
                try {
                    if (cursor.moveToFirst()) {
                        do {
                            String name = cursor.getString(0);
                            String encrypted = cursor.getString(1);
                            d1.myMethod(this);
                            decrypted1 = d1.encryppass(encrypted);
                            Log.i("Title:     ", name);
                            Log.i("Password:  ", decrypted1);
                            Toast.makeText(getApplicationContext(), decrypted1, Toast.LENGTH_LONG).show();
                        } while (cursor.moveToNext());
                    }
                } catch (NullPointerException exception) {
                    exception.printStackTrace();
                }
            }

Decryption 2 class -> and pass that above statement here "private String pinToMd5 = d3.decrypted1;"

public class Decrypt2 {
    private Cipher cipher;
    MainActivity d3;
    private String pinToMd5;
    public String pin_to_md5;SecretKeySpec secretKeySpe = new SecretKeySpec(this.pin_to_md5.getBytes(), "AES");

    public Decrypt2(MainActivity mainActivity)
        {
        d3 = mainActivity;
        pinToMd5 = d3.decrypted1;
        pin_to_md5 = MD5(pinToMd5);
        }

</details>


# 答案1
**得分**: 1

从我所看到的情况来看,问题可能出在这里:

```java
MainActivity d3 = new MainActivity();
//private String pinToMd5 = &quot;PaSsW0rD&quot;; //use this to test my code to confirm any changes doesn&#39;t break the logic -&gt; this works
private String pinToMd5 = d3.decrypted1;

你在这里创建了 MainActivity 类的新实例,并期望从堆栈中的那个实例获取对象。尝试将上下文或引用从现有的 MainActivity 类传递并在 Decrypt 类中进行分配:

InputStream instream = getContentResolver().openInputStream(singleUri);
/*Reference from MainActivity class*/
Decrypt2 d2 = new Decrypt2(this);
String decrypted2 = d2.encrypnote(instream);

在 MainActivity 类中,你正在使用:

public String decrypted1 = &quot;&quot;;

作为属性。但是在 onClick 方法中,你创建了包含加密字符串的局部变量:

d1.myMethod(this);
//this code here
String decrypted1 = d1.encryppass(encrypted);
Log.i(&quot;Title:     &quot;, name);

局部变量只在方法内部存在,而不在类中存在,所以不要在方法中创建新变量,而是使用上面的变量。移除那个 String ... 并将代码改为类似这样的形式:

d1.myMethod(this);
//this code here
decrypted1 = d1.encryppass(encrypted);
Log.i(&quot;Title:     &quot;, name);

第二个问题是这里的代码:

public String pin_to_md5 = MD5(pinToMd5);

在 pinToMd5 属性初始化之前调用了 MD5 方法。如果你在类的初始化时需要使用它,那么对代码进行如下更改:

public class Decrypt2 {
//your code...

MainActivity d3;
//private String pinToMd5 = &quot;PaSsW0rD&quot;; //use this to test my code to confirm any changes doesn&#39;t break the logic -&gt; this works
private String pinToMd5;
//remove MD5(pinToMd5);
public String pin_to_md5;
SecretKeySpec secretKeySpe;

public Decrypt2(MainActivity mainActivity)
{
    d3 = mainActivity;
    pinToMd5 = d3.decrypted1;
    pin_to_md5 = MD5(pinToMd5);
    secretKeySpe = new SecretKeySpec(this.pin_to_md5.getBytes(), &quot;AES&quot;);
}
//your code...
}

第三个问题是在 MainActivity 中你使用了:

public String decrypted1 = &quot;&quot;;

作为属性。但是在 onClick 方法中,你创建了包含加密字符串的局部变量:

d1.myMethod(this);
//this code here
String decrypted1 = d1.encryppass(encrypted);
Log.i(&quot;Title:     &quot;, name);

局部变量只在方法内部存在,而不在类中存在,所以不要在方法中创建新变量,而是使用上面的变量。移除那个 String ... 并将代码改为类似这样的形式:

d1.myMethod(this);
//this code here
decrypted1 = d1.encryppass(encrypted);
Log.i(&quot;Title:     &quot;, name);
英文:

From what I can see the issue might be here

MainActivity d3 = new MainActivity();
    //private String pinToMd5 = &quot;PaSsW0rD&quot;; //use this to test my code to confirm any changes doesn&#39;t break the logic -&gt; this works
    private String pinToMd5 = d3.decrypted1;

You are creating new instance from MainActivity class and expect the object from the one presented in the stack. Try passing the context or reference from the existing main activity class and assign it in the Decrypt class

    InputStream instream = getContentResolver().openInputStream(singleUri);
/*Reference from MainActivity class*/
                        Decrypt2 d2 = new Decrypt2(this);
                        String decrypted2 = d2.encrypnote(instream);

public class Decrypt2 {
//your code...

MainActivity d3;
    //private String pinToMd5 = &quot;PaSsW0rD&quot;; //use this to test my code to confirm any changes doesn&#39;t break the logic -&gt; this works
private String pinToMd5;

public Decrypt2(MainActivity mainActivity)
{
    d3 = mainActivity;
    pinToMd5 = d3.decrypted1;
}
//your code...
}

Not sure this will fix your problem completely 在Java中在类之间传递空字符串的问题

Second issue is this code here:

public String pin_to_md5 = MD5(pinToMd5);

You are invoking the method MD5 before the pinToMd5 property is initialised? when did you need to use that class? If on the initialization of the class than do this changes in the code:

public class Decrypt2 {
//your code...

MainActivity d3;
    //private String pinToMd5 = &quot;PaSsW0rD&quot;; //use this to test my code to confirm any changes doesn&#39;t break the logic -&gt; this works
private String pinToMd5;
//remove MD5(pinToMd5);
public String pin_to_md5;
SecretKeySpec secretKeySpe;


public Decrypt2(MainActivity mainActivity)
{
    d3 = mainActivity;
    pinToMd5 = d3.decrypted1;
    pin_to_md5 = MD5(pinToMd5);
    secretKeySpe = new SecretKeySpec(this.pin_to_md5.getBytes(), &quot;AES&quot;);

}
//your code...
}

Third issue
In MainActivity you are using

public String decrypted1 = &quot;&quot;;

as property. But then in onClick method you are creating local variable that contains the encrypted string

     d1.myMethod(this);
//this code here
     String decrypted1 = d1.encryppass(encrypted);
    
     Log.i(&quot;Title:     &quot;, name);

Local values live only inside the method but not in the class so instead creating new variable in the method use the one from above. Remove that String .. and make the code smth like this

d1.myMethod(this);
//this code here
decrypted1 = d1.encryppass(encrypted);
        
Log.i(&quot;Title:     &quot;, name);

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

发表评论

匿名网友

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

确定